gpt4 book ai didi

调整大小后的 WPF GridSplitter

转载 作者:行者123 更新时间:2023-12-02 01:37:42 30 4
gpt4 key购买 nike

一旦使用 gridsplitter 调整网格大小,当其他行折叠时,该行 * 将不会回收空间。

我在主详细信息 View 中有以下三行网格。顶部有一个数据网格,中间有一个分割器,最后一行有一个内容控制 View 。拆分器上有一个关闭按钮,用于折叠细节。除了用户使用 gridsplitter 调整大小之外,这一切都有效。

    <Grid Margin="3,0">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Style="{StaticResource CollapsableRow}"/><!-- Splitter Here -->
<RowDefinition Style="{StaticResource CollapsableRow}"/>
</Grid.RowDefinitions>

GridSplitter 样式:

    <Style x:Key="gridSplitterStyle" TargetType="{x:Type GridSplitter}">
<Setter Property="Visibility" Value="{Binding IsItemSelected, Converter={StaticResource BoolToShow},ConverterParameter='Visible|Collapsed'}" />
<Setter Property="Width" Value="Auto"/>
<Setter Property="Height" Value="14"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="Border.BorderBrush" Value="#FF6593CF" />
<Setter Property="Border.BorderThickness" Value="0,1,0,0" />
<Setter Property="UIElement.SnapsToDevicePixels" Value="True" />
<Setter Property="UIElement.Focusable" Value="False" />
<Setter Property="Control.Padding" Value="7,7,7,7" />
<Setter Property="Cursor" Value="SizeNS" /></Style>

就像我说的,除非使用 gridsplitter 来调整大小,否则折叠可以正常工作。之后空白仍然存在。

编辑:H.B. codenaked 有简单且一致的建议,因此我尝试在数据触发器中未成功实现它们:

<Style x:Key="CollapsableRow" TargetType="{x:Type RowDefinition}">
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedItem, Converter={StaticResource IsNullConverter}}" Value="True">
<Setter Property="RowDefinition.Height" Value="0"/>
</DataTrigger>
<DataTrigger Binding="{Binding SelectedItem, Converter={StaticResource IsNullConverter}}" Value="False">
<Setter Property="RowDefinition.Height" Value="Auto"/>
</DataTrigger>
</Style.Triggers>
</Style>

最佳答案

由于网格分割器和细节已经被隐藏,可见性是重置下一行定义高度的明显选择。

 /// <summary>
/// Grid splitter that show or hides the following row when the visibility of the splitter is changed.
/// </summary>
public class HidableGridSplitter : GridSplitter {

GridLength height;

public HidableGridSplitter()
{
this.IsVisibleChanged += HideableGridSplitter_IsVisibleChanged;
this.Initialized += HideableGridSplitter_Initialized;
}

void HideableGridSplitter_Initialized(object sender, EventArgs e)
{
//Cache the initial RowDefinition height,
//so it is not always assumed to be "Auto"
Grid parent = base.Parent as Grid;
if (parent == null) return;
int rowIndex = Grid.GetRow(this);
if (rowIndex + 1 >= parent.RowDefinitions.Count) return;
var lastRow = parent.RowDefinitions[rowIndex + 1];
height = lastRow.Height;
}

void HideableGridSplitter_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
Grid parent = base.Parent as Grid;
if (parent == null) return;

int rowIndex = Grid.GetRow(this);

if (rowIndex + 1 >= parent.RowDefinitions.Count) return;

var lastRow = parent.RowDefinitions[rowIndex + 1];

if (this.Visibility == Visibility.Visible)
{
lastRow.Height = height;
}
else
{
height = lastRow.Height;
lastRow.Height = new GridLength(0);
}

}

关于调整大小后的 WPF GridSplitter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5955186/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com