我有一个WPF页面,上面有一个网格。
有三行。第0行包含带Height="*"
的GridView。第1行包含一个带Height="auto"
的GridSplitter。第2行包含Height="2*"
的详细信息表单。
这就是事情 - 我有一个按钮,可以切换详细信息表单的可见性。这很好用。除了它只隐藏第2行中的表单,它不会扩展第0行中的网格以填充空格。我想要的是按钮在行0中切换GridView以占用所有空间,然后切换回原来的位置。
显然,使用行内表格的可见性无法实现我想要的效果。
但我需要玩什么?
答案 0 :(得分:6)
假设我有这个XAML布局:
<Grid Name="MyGrid">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<MyControl1 ... Grid.Row="0" />
<GridSplitter Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Stretch" ShowsPreview="True" Height="5" />
<MyControl2 ... Grid.Row="2" />
</Grid>
然后我可以使用此代码隐藏第二个控件(折叠拆分器)(相当于在XAML中设置Height="0"
):
MyGrid.RowDefinitions[2].Height = new GridLength(0);
使用此代码取消折叠(相当于在XAML中设置Height="1*"
,这是RowDefinition的默认设置):
MyGrid.RowDefinitions[2].Height = new GridLength(1, GridUnitType.Star);
当用户移动分割器时,这就是分离器的功能。
答案 1 :(得分:3)
这个GridExpander控件继承了GridSpliter,可能是你正在寻找的工作。为了编写我在WPF中自己使用的原始Silverlight版本,可以转到Shemesh。我发现自己几乎在所有尝试使用GridSplitter的地方都想要这个功能,所以它非常方便。
答案 2 :(得分:2)
我必须在我自己的应用程序中引入一个Attached Dependency Property来处理它:
<Grid c:GridSplitterController.Watch="{Binding ElementName=GS_DetailsView}">
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="200" />
</Grid.RowDefinitions>
<SomeControl Grid.Row="0" />
<GridSplitter x:Name="GS_DetailsView"
Height="4"
Grid.Row="1"
VerticalAlignment="Top"
HorizontalAlignment="Stretch"
ResizeBehavior="PreviousAndCurrent"
ResizeDirection="Rows"
Visibility="{Binding ShowDetails,
Converter={StaticResource boolvis}}" />
<OtherControl Grid.Row="1"
Margin="0,4,0,0"
Visibility="{Binding ShowDetails,
Converter={StaticResource boolvis}}" />
</Grid>
首先在DependencyObject
上定义合适的附加属性:
public static GridSplitter GetWatch(DependencyObject obj)
{
return (GridSplitter)obj.GetValue(WatchProperty);
}
public static void SetWatch(DependencyObject obj, GridSplitter value)
{
obj.SetValue(WatchProperty, value);
}
public static readonly DependencyProperty WatchProperty =
DependencyProperty.RegisterAttached(
"Watch",
typeof(GridSplitter),
typeof(DependencyObject),
new UIPropertyMetadata(null, OnWatchChanged));
然后听IsVisibleChanged
:
private static void OnWatchChanged(DependencyObject obj,
DependencyPropertyChangedEventArgs e)
{
if (obj == null) return;
if (obj is Grid)
{
var grid = obj as Grid;
var gs = e.NewValue as GridSplitter;
if (gs != null)
{
gs.IsVisibleChanged += (_sender, _e) =>
{
UpdateGrid(
grid,
(GridSplitter)_sender,
(bool)_e.NewValue,
(bool)_e.OldValue);
};
}
}
}
在您观察这些更改后,您需要保存或恢复正在观看的行中的GridLength
值(为简洁起见,我只包括行):
// Given: static Dictionary<DependencyObject, GridLength> oldValues;
private static void UpdateGrid(Grid grid, GridSplitter gridSplitter, bool newValue, bool oldValue)
{
if (newValue)
{
// We're visible again
switch (gridSplitter.ResizeDirection)
{
case GridResizeDirection.Columns:
break;
case GridResizeDirection.Rows:
int ridx = (int)gridSplitter.GetValue(Grid.RowProperty);
var prev = grid.RowDefinitions.ElementAt(GetPrevious(gridSplitter, ridx));
var curr = grid.RowDefinitions.ElementAt(GetNext(gridSplitter, ridx));
if (oldValues.ContainsKey(prev) && oldValues.ContainsKey(curr))
{
prev.Height = oldValues[prev];
curr.Height = oldValues[curr];
}
break;
}
}
else
{
// We're being hidden
switch (gridSplitter.ResizeDirection)
{
case GridResizeDirection.Columns:
break;
case GridResizeDirection.Rows:
int ridx = (int)gridSplitter.GetValue(Grid.RowProperty);
var prev = grid.RowDefinitions.ElementAt(GetPrevious(gridSplitter, ridx));
var curr = grid.RowDefinitions.ElementAt(GetNext(gridSplitter, ridx));
switch (gridSplitter.ResizeBehavior)
{
// Naively assumes only one type of collapsing!
case GridResizeBehavior.PreviousAndCurrent:
oldValues[prev] = prev.Height;
prev.Height = new GridLength(1.0, GridUnitType.Star);
oldValues[curr] = curr.Height;
curr.Height = new GridLength(0.0);
break;
}
break;
}
}
}
剩下的就是GetPrevious
和GetNext
的适当实现:
private static int GetPrevious(GridSplitter gridSplitter, int index)
{
switch (gridSplitter.ResizeBehavior)
{
case GridResizeBehavior.PreviousAndNext:
case GridResizeBehavior.PreviousAndCurrent:
return index - 1;
case GridResizeBehavior.CurrentAndNext:
return index;
case GridResizeBehavior.BasedOnAlignment:
default:
throw new NotSupportedException();
}
}
private static int GetNext(GridSplitter gridSplitter, int index)
{
switch (gridSplitter.ResizeBehavior)
{
case GridResizeBehavior.PreviousAndCurrent:
return index;
case GridResizeBehavior.PreviousAndNext:
case GridResizeBehavior.CurrentAndNext:
return index + 1;
case GridResizeBehavior.BasedOnAlignment:
default:
throw new NotSupportedException();
}
}
答案 3 :(得分:1)
万一有人想要一个纯粹的XAML解决方案,我能够想到一种使用样式,设置器和触发器来隐藏分隔符和相关行的方法。
我为样式使用了静态资源,该静态资源用于在设置特定的绑定布尔值时更改Height
和MaxHeight
。
<Style x:Key="showRow" TargetType="{x:Type RowDefinition}">
<Style.Setters>
<Setter Property="Height" Value="*"/>
</Style.Setters>
<Style.Triggers>
<DataTrigger Binding="{Binding MyShowRowBool}" Value="False">
<DataTrigger.Setters>
<Setter Property="Height" Value="0"/>
<Setter Property="MaxHeight" Value="0"/>
</DataTrigger.Setters>
</DataTrigger>
</Style.Triggers>
</Style>
我只是将样式应用于相关的行定义,它就像一种魅力:
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Style="{StaticResource showRow}"/>
<RowDefinition Style="{StaticResource showRow}"/>
</Grid.RowDefinitions>
值得注意的是,我尝试了没有MaxHeight属性的情况,并且无法正常折叠。添加它似乎对我来说是成功的秘诀。