2017年11月15日(水) [長年日記]
■ [c#] WPFでGridSplitterを使ったときに行の高さがおかしくなる問題への対処
問題
次のXAMLのように、Gridの行にMinHeightを設定したWPFのウィンドウは大体思った通りに動作する。
<Window x:Class="GridSplitter.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:GridSplitter"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" MinHeight="250">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" MinHeight="80"/>
<RowDefinition Height="5"/>
<RowDefinition Height="*" MinHeight="80"/>
</Grid.RowDefinitions>
<TextBox Grid.Row="0"
Text="1
2
3
4
5
6
7
8
8
9"
VerticalScrollBarVisibility="Visible"/>
<GridSplitter Grid.Row="1" HorizontalAlignment="Stretch"
ResizeBehavior="PreviousAndNext"/>
<TextBox Grid.Row="2"
Text="1
2
3
4
5
6
7
8
8
9"
VerticalScrollBarVisibility="Visible"/>
</Grid>
</Window>
しかし、
- 中央のGridSplitterをマウスで一番下までドラッグする。
- マウスドラッグによりウィンドウの高さを一番小さくする。
という操作をすると、下側のTextBoxがウィンドウをはみ出てしまい下スクロール用のボタンが表示されなくなってしまう。

対処
Gridの行にMinHeightを設定するのをやめ、TextBoxを「高さ固定の行」と「高さ可変(*を指定)の行」にまたがって配置するようにすると、上記の操作をしてもTextBoxがウィンドウをはみでなくなった。
<Window x:Class="GlidSplitter2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:GlidSplitter2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" MinHeight="250">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="80"/>
<RowDefinition Height="*"/>
<RowDefinition Height="5"/>
<RowDefinition Height="*"/>
<RowDefinition Height="80"/>
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Grid.RowSpan="2"
Text="1
2
3
4
5
6
7
8
8
9"
VerticalScrollBarVisibility="Visible"/>
<GridSplitter Grid.Row="2" HorizontalAlignment="Stretch"
ResizeBehavior="PreviousAndNext"/>
<TextBox Grid.Row="3" Grid.RowSpan="2"
Text="1
2
3
4
5
6
7
8
8
9"
VerticalScrollBarVisibility="Visible"/>
</Grid>
</Window>

ただし、WindowのMinHeightを小さすぎる値(高さを指定している行が入りきらなくなる値?)に設定するとおかしな挙動が発生するので注意。
[ツッコミを入れる]