动态更改GroupBox的内容

本文关键字:GroupBox 动态 | 更新日期: 2023-09-27 18:26:16

我有一个GroupBox。此盒子包含一个UserControl

<GroupBox Header="NewsBox">
    <GroupBox.Content>
        <NewsDay:NewsDayControl DataContext="{Binding SelectedNews}"/>
    </GroupBox.Content>
</GroupBox>

现在,我想根据TabControl的选定选项卡动态更改GroupBox的内容。

<DataTrigger Binding="{Binding  ElementName=TabControl, Path=SelectedIndex}" Value="0">
    <Setter Property="Visibility" Value="Hidden" />
</DataTrigger>

如果选择了索引为1的选项卡,则应显示另一个UserControl

<Imprint:ImprintControl DataContext="{Binding SelectedImprint}"/>

我该怎么做?

动态更改GroupBox的内容

您可以将Style与这样的触发器一起使用。

<GroupBox Header="NewsBox">
    <GroupBox.Style>
        <Style TargetType="GroupBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding  ElementName=TabControl, Path=SelectedIndex}" Value="0">
                    <Setter Property="Content">
                        <Setter.Value>
                            <NewsDay:NewsDayControl DataContext="{Binding SelectedNews}"/>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding  ElementName=TabControl, Path=SelectedIndex}" Value="1">
                    <Setter Property="Content">
                        <Setter.Value>
                            <Imprint:ImprintControl DataContext="{Binding SelectedImprint}"/>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </GroupBox.Style>
</GroupBox>