Чтобы вернуть GridSplitter в исходное положение, свяжите высоту RowDefinition с обоими Grid.RowDefinitions между GridSplitter.Обратите внимание, я также обычно помещаю GridSplitter в его собственную Grid RowDefinition с помощью Height = Auto.
Я включил привязку для команды RowDefinition Height and Button ниже, все остальное - просто некоторые данные, которые я использовал для отображения.Это также можно сделать с помощью привязки кода для события «Нажатие кнопки».
Вот код XAML:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="{Binding Path=Row0GridHeight, Mode=TwoWay}"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="{Binding Path=Row2GridHeight, Mode=TwoWay}" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<TextBox Text="Enter text here"/>
<Button Content="Click"
VerticalAlignment="Top"
Height="23"
Command="{Binding Path=ToggleViewCommand}"/>
</StackPanel>
<GridSplitter Grid.Row="1"
ResizeBehavior="PreviousAndNext"
HorizontalAlignment="Stretch"
ResizeDirection="Rows"
Height="5"/>
<Grid Grid.Row="2">
<ListBox>
<ListBox.Items>
<TextBlock Text="Choice 1"/>
<TextBlock Text="Choice 2"/>
<TextBlock Text="Choice 3"/>
<TextBlock Text="Choice 4"/>
<TextBlock Text="Choice 5"/>
</ListBox.Items>
</ListBox>
</Grid>
</Grid>
Вот привязка высоты RowDefinition:
private GridLength _row0GridHeight = new GridLength(1, GridUnitType.Star);
public GridLength Row0GridHeight
{
get
{
return _row0GridHeight;
}
set
{
_row0GridHeight = value;
NotifyPropertyChanged("Row0GridHeight");
}
}
private GridLength _row2GridHeight = new GridLength(0, GridUnitType.Star);
public GridLength Row2GridHeight
{
get
{
return _row2GridHeight;
}
set
{
_row2GridHeight = value;
NotifyPropertyChanged("Row2GridHeight");
}
}
Вот привязка Команды от Button (реализует ICommand):
private void ExecuteToggleViewCommand(Object args)
{
if ( _row2GridHeight.Value == 1)
{
Row2GridHeight = new GridLength(0, GridUnitType.Star);
}
else
{
Row0GridHeight = new GridLength(1, GridUnitType.Star);
Row2GridHeight = new GridLength(1, GridUnitType.Star);
}
}