C# 网格拆分器自动调整大小

C# 网格拆分器自动调整大小,c#,wpf,xaml,grid,gridsplitter,C#,Wpf,Xaml,Grid,Gridsplitter,我发现GridSplitter有问题 简单示例代码: <Grid x:Name="MainGrid"> <Grid.RowDefinitions> <RowDefinition Height="100"></RowDefinition> <RowDefinition Height="2"></RowDefinition> <RowDefinition Height

我发现
GridSplitter有问题

简单示例代码:

<Grid x:Name="MainGrid">
    <Grid.RowDefinitions>
        <RowDefinition Height="100"></RowDefinition>
        <RowDefinition Height="2"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <Grid x:Name="TopGrid" Grid.Row="0"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Red"/>
    <GridSplitter ResizeDirection="Rows" Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Height="2" Background="Black" />
    <Grid x:Name="BottomGrid" Grid.Row="2" HorizontalAlignment="Stretch"  Background="Aquamarine" VerticalAlignment="Stretch"/>
</Grid>

您可以使用多重绑定来实现您想要的

对于每个行定义,您设置一个最小高度,该高度绑定到其内容,实际高度如下:

<RowDefinition Height="100">
    <RowDefinition.MinHeight>
        <MultiBinding Converter="{StaticResource ResourceKey=CalcAll}">
            <Binding ElementName="firstElementInThisRow" Path="ActualHeight"></Binding>
            <Binding ElementName="secondElementInThisRow" Path="ActualHeight"></Binding>
            <Binding ElementName="thirdElementInThisRow" Path="ActualHeight"></Binding>
            <Binding ElementName="fourthElementInThisRow" Path="ActualHeight"></Binding>
        </MultiBinding>
    </RowDefinition.MinHeight>
</RowDefinition>
每次展开控件时,其实际高度都会发生更改,并且绑定会得到父行定义的更新->最小高度更改

但是如果控件垂直对齐要拉伸,则不能设置一个,因为这样实际高度就不会因展开而改变

编辑:由于我现在能想到的唯一属性是DesiredSize.Height-property,因此不能使用绑定(如果DesiredSize.Height-value更改,绑定将不会更新)。 但是,也许您可以使用类型为double的属性(我们称之为MinHeightRowOne),该属性在其setter中引发PropertyChanged事件,并绑定到第一行MinHeight(每行一个属性):

public double\u minHeightRowOne;
公共双MinHeightRowOne
{
得到
{
返回_minHeightRowOne;
}
设置
{
_minHeightRowOne=值;
房地产变更(“MinHeightRowOne”);
}
}
公共事件属性更改事件处理程序属性更改;
公共void OnPropertyChanged(字符串名称)
{
if(PropertyChanged!=null)
{
PropertyChanged(此,新PropertyChangedEventArgs(名称));
}
}


现在将此EventHandler添加到第一行中每个控件的SizeChanged事件(每行一个处理程序):

private List oldVals=new List();
私有void元素InRowoneSizeChanged(对象发送方,SizeChangedEventArgs e)
{
FrameworkElement元素=(FrameworkElement)发送方;
MinHeightRowOne-=oldVals.Find(kvp=>kvp.Key==elem.Name).Value;
元素度量(新大小(int.MaxValue,int.MaxValue));
MinHeightRowOne+=元素DesiredSize.Height;
Remove(oldVals.Find(kvp=>kvp.Key==elem.Name));
添加(新的KeyValuePair(elem.Name,elem.DesiredSize.Height));
}
通过此操作,每次控件的大小更改(应包括展开或折叠项)时,行的最小高度都会更新


请注意,每个控件必须具有唯一的名称才能执行此操作。

此解决方案的问题在于“ActualHeight”始终是行的高度。如果我将行的高度设置为“100”,则此行中数据网格的实际高度也是“100”。。。它不包括可滚动的内容。你能在你的问题中添加你的xaml吗?嗯,它不能真正发挥作用,因为“DesiredSize.Height”也不会改变。获得与“实际高度”相同的值。。。“100”如果我将行的“高度”设置为“100”,这很奇怪,实际上这就是所需的大小(即使元素比其父元素高,也能获得边界)…在我的例子中,它确实工作得很好(偶数宽度行高=“100”)。难道你忘了
elem.Measure(新大小(int.MaxValue,int.MaxValue))
<RowDefinition Height="100">
    <RowDefinition.MinHeight>
        <MultiBinding Converter="{StaticResource ResourceKey=CalcAll}">
            <Binding ElementName="firstElementInThisRow" Path="ActualHeight"></Binding>
            <Binding ElementName="secondElementInThisRow" Path="ActualHeight"></Binding>
            <Binding ElementName="thirdElementInThisRow" Path="ActualHeight"></Binding>
            <Binding ElementName="fourthElementInThisRow" Path="ActualHeight"></Binding>
        </MultiBinding>
    </RowDefinition.MinHeight>
</RowDefinition>
<RowDefinition Height="100" MinHeight="{Binding Path=MinHeightRowOne}"/>