你可以嘗試這樣的:
<Grid Background="Green">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition>
<RowDefinition.Style>
<Style TargetType="{x:Type RowDefinition}">
<Setter Property="Height"
Value="0.25*" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=secondRect,
Path=Visibility}"
Value="Collapsed">
<Setter Property="Height"
Value="0" />
</DataTrigger>
</Style.Triggers>
</Style>
</RowDefinition.Style>
</RowDefinition>
</Grid.RowDefinitions>
<!-- Row 1 -->
<Rectangle Grid.Row="0"
Fill="Blue" />
<!-- Row 2 -->
<Rectangle x:Name="secondRect"
Grid.Row="1"
Fill="Tomato" />
</Grid>
所以你在第二網格行設置Style.Trigger
,以檢查其中是否包含元素是Collapsed
如果是這樣設置它的高度爲「0」,如果不是「0.25 *」,而Grid
第1行有它的Height
作爲*或「所有剩餘空間」,這將配合這種罰款。
備選:
您可以用Grid.RowSpan
做得一樣好喜歡你所提到的。
<Grid Background="Green">
<Grid.RowDefinitions>
<RowDefinition Height="0.75*" />
<RowDefinition Height="0.25*" />
</Grid.RowDefinitions>
<!-- Row 1 -->
<Rectangle Grid.Row="0"
Fill="Blue">
<Rectangle.Style>
<Style TargetType="{x:Type Rectangle}">
<Setter Property="Grid.RowSpan"
Value="1" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=secondRect,
Path=Visibility}"
Value="Collapsed">
<Setter Property="Grid.RowSpan"
Value="2" />
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
<!-- Row 2 -->
<Rectangle x:Name="secondRect"
Grid.Row="1"
Fill="Tomato"
Visibility="Collapsed" />
</Grid>
現在,你有第1'行的「元素」的Trigger
比Grid.RowDefinition
,當你發現第二行的元素變成Collapsed
切換的第一個的行跨度爲2否則它停留在1
來源
2013-06-30 21:18:30
Viv
您爲我打開了一個新的領域:直接在XAML中修改樣式和屬性。非常感謝你。 – Sturm