我遇到了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>
这会创建两个由GridSplitter
垂直分隔的网格。
我想要实现的是,GridSplitter
自动与Grid的内容对齐。
例如,如果我在底部网格中有一个可折叠元素,我希望顶部网格变大,如果我折叠元素。如果我展开它,顶部Grid
应该变小。
我该怎么做?稍后我会有4个Grids
和3个GridSplitter
...所以解决方案也应该适用于多个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>
答案 0 :(得分: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会更新 - &gt;父类RowDefinition的MinHeight更改。
但如果控件VerticalAlignment为Stretch,你就不能设置一个,因为那么ActualHeight不会通过扩展来改变。
编辑:由于我现在能想到的唯一属性是DesiredSize.Height属性,因此您无法使用Binding(如果DesiredSize.Height值更改,绑定将不会更新) 。 但是,你可以使用double类型的属性(让我们调用MinHeightRowOne),它在它的setter中引发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就会更新(应该包括扩展或折叠项目)。
请注意,每个控件都必须具有唯一的名称才能使其正常工作。