根据选定的 TabItem wpf 更改 windows 高度

change windows height based on TabItem Selected wpf

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

<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 中,当我更改选项卡代码时可以正常工作,但是当我编译我的代码时无法正常工作

您可以使用 DoubleAnimation 来设置 window 的 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>