1

我遇到了问题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>

这将创建两个由 . 垂直分隔的 Grid GridSplitter

我想要实现的是,GridSplitter自动对齐网格的内容。例如,如果我在底部网格中有一个可折叠元素,我希望顶部网格在折叠元素时变得更大。如果我扩大它,顶部Grid应该变小。

我该怎么做呢?稍后我将有 4Grids和 3GridSplitter的...所以该解决方案也应该适用于多个GridSplitter

[编辑]:

我的xml:

 <Grid x:Name="MainGrid">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="100">
            <RowDefinition.MinHeight>
                <MultiBinding Converter="{StaticResource ResourceKey=MultiValueConverter}">
                    <Binding ElementName="dgStapelliste" Path="ActualHeight"></Binding>
                </MultiBinding>
            </RowDefinition.MinHeight>
        </RowDefinition>
        <RowDefinition Height="1"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition >           
    </Grid.RowDefinitions>

    <Grid Grid.Row="0" Grid.Column="0" x:Name="Test">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="200"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <WPF:DataGrid GridHeaderContent="{Binding StapelListe.HeaderText}" SelectedItem="{Binding StapelListe.SelectedItem}" Grid.Row="0" Grid.Column="0" x:Name="dgStapelliste" HorizontalAlignment="Stretch" ItemsSource="{Binding StapelListe, Mode=OneWay,  UpdateSourceTrigger=PropertyChanged}"/>
        <WPF:DataGrid GridHeaderContent="{Binding EinzelListe.HeaderText}" Grid.Row="0" Grid.Column="1" x:Name="dgEinzelliste" HorizontalAlignment="Stretch"  ItemsSource="{Binding EinzelListe, Mode=OneWay}"/>

        <GridSplitter Grid.Column="0" Width="1" VerticalAlignment="Stretch" Background="Black" />
    </Grid>

    <Grid Grid.Row="2" Grid.Column="0" Grid.RowSpan="2">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="200"/>
        </Grid.ColumnDefinitions>

        <WPF:DataGrid  GridHeaderContent="{Binding Anforderungsliste.HeaderText}" Grid.Column="0" x:Name="dgAnforderungsliste" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding Anforderungsliste, Mode=OneWay}"/>

        <GridSplitter Grid.Column="0" Width="1" VerticalAlignment="Stretch" Background="Black" />
    </Grid>
    <GridSplitter Grid.Column="0" Grid.Row="1" Height="1" ResizeDirection="Rows"  VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="Black" x:Name="testSplitter" />
</Grid>
4

1 回答 1

3

您可以使用 MultiBinding 来实现您想要的。

对于每个 RowDefinition,您设置一个绑定到其内容 ActualHeight 的 MinHeight,如下所示:

<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>

您的转换器可能如下所示:

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    double result = 0.0;
    foreach (object item in values)
    {
        result += System.Convert.ToDouble(item);
    }
    return result;
}

public object[] ConvertBack(object values, Type[] targetType, object parameter, CultureInfo culture)
{
    return null;
}

每次展开控件时,它的 ActualHeight 都会发生变化,并且 Binding 会更新 -> 父级 RowDefinition 的 MinHeight 会发生变化。

但是,如果控件 VerticalAlignment 设置为 Stretch,则不能设置一个,因为 ActualHeight 不会因扩展而改变。

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

public double _minHeightRowOne;
public double MinHeightRowOne
{
    get
    {
        return _minHeightRowOne;
    }
    set
    {
        _minHeightRowOne = value;
        OnPropertyChanged("MinHeightRowOne");
    }
}

public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
}


<RowDefinition Height="100" MinHeight="{Binding Path=MinHeightRowOne}"/>

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

private List<KeyValuePair<string,double>> oldVals = new List<KeyValuePair<string,double>>();

private void ElementInRowOneSizeChanged(object sender, SizeChangedEventArgs e)
{
    FrameworkElement elem = (FrameworkElement)sender;
    MinHeightRowOne -= oldVals.Find(kvp => kvp.Key == elem.Name).Value;
    elem.Measure(new Size(int.MaxValue, int.MaxValue));
    MinHeightRowOne += elem.DesiredSize.Height;
    oldVals.Remove(oldVals.Find(kvp => kvp.Key == elem.Name));
    oldVals.Add(new KeyValuePair<string, double>(elem.Name, elem.DesiredSize.Height));
}

通过这个,每次控件的大小更改(应包括展开或折叠项目)时,行的 MinHeight 都会得到更新。

请注意,每个控件都必须具有唯一的名称才能使其正常工作。

于 2013-01-07T10:59:42.210 回答