根据Expander的IsExpanded属性值的变化动态设计Control的size

时间:2021-06-16 19:53:31

简要说明:

当Expander 的IsExpanded属性为“True” 时给控件设个尺寸(此处为高度),当为“False”时给控件设另外一个值。

知识点:数据绑定、Style和Trigger

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition>
                <RowDefinition.Style>
                    <Style TargetType="RowDefinition">
                        <Setter Property="Height" Value="25" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=testExpander,Path=IsExpanded}" Value="True">
                                <Setter Property="Height" Value="205" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </RowDefinition.Style>
            </RowDefinition>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
            
        <Expander Header="Haha"  x:Name="testExpander" />
        <StackPanel Grid.Row="1" >
            <TabControl>
                <TabItem Header="HaHaTabItem" >
                    <ListBox>
                        <ListBoxItem Content="Blue" />
                        <ListBoxItem Content="Black" />
                        <ListBoxItem Content="Red" />
                    </ListBox>
                 </TabItem>
            </TabControl>
        </StackPanel>
    </Grid>