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>

これにより、 で垂直方向に区切られた 2 つのグリッドが作成されますGridSplitter

私が達成したいのはGridSplitter、グリッドのコンテンツに自動的に整列することです。たとえば、下のグリッドに折りたたみ可能な要素がある場合、要素を折りたたむと上のグリッドが大きくなるようにします。広げたら上Gridが小さくなるはず。

どうすればいいですか?後で私はGrids3 つGridSplitterの ´ を持つ 4 を持ちます... したがって、ソリューションは複数GridSplitterの ´ でも同様に機能するはずです。

[編集]:

私のxaml:

 <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ごとに、その内容にバインドされるMinHeightを設定します。ActualHeightは次のようになります。

<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に制御する場合は、1を設定できません。これは、ActualHeightが展開しても変更されないためです。

編集:私が今考えることができる唯一のプロパティはDesiredSize.Heightプロパティであるため、Bindingを使用することはできません(DesiredSize.Height値が変更された場合、バインディングは更新されません)。しかし、おそらく、double型のプロパティ(MinHeightRowOneと呼びましょう)を使用して、セッターでPropertyChangedイベントを発生させ、最初の行MinHeight(各行に1つのプロパティ)にバインドすることができます。

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に追加します(各行に1つのハンドラー)。

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 に答える