基于TabItem Selected wpf更改窗口高度

时间:2018-10-26 07:23:20

标签: c# wpf binding datatrigger propertychanged

嗨,我想当用户选择Tabitem窗口高度变化时,这是我的代码:

<Windows.Style>
        <Style TargetType="Windows">
            <Setter Property="Height" Value="220" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=tabc,Path=SelectedIndex, UpdateSourceTrigger=PropertyChanged, Mode=Twoway}" Value="1">
                    <Setter Property="Height" Value="400" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Windows.Style>

在Visual Studio中,当我更改选项卡代码时可以正常工作,但是在编译代码时却无法工作

1 个答案:

答案 0 :(得分:2)

您可以使用DoubleAnimation来设置窗口的Height。可以按预期工作:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="300" Width="300">
    <Window.Style>
        <Style TargetType="Window">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=tabc, Path=SelectedIndex}" Value="1">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation To="400" Duration="0:0:0" Storyboard.TargetProperty="Height" />
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                    <DataTrigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation To="220" Duration="0:0:0" Storyboard.TargetProperty="Height" />
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.ExitActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Style>
    <Grid>
        <TabControl x:Name="tabc">
            <TabItem Header="1">
                <TextBlock>first tab</TextBlock>
            </TabItem>
            <TabItem Header="2">
                <TextBlock>second tab</TextBlock>
            </TabItem>
        </TabControl>
    </Grid>
</Window>