为什么我的 DataTrigger 不更新 TabControl 的 SelectedIndex 属性?

发布于 2024-12-27 18:00:05 字数 976 浏览 1 评论 0原文

我有一个 TabControl ,它的 SelectedIndex 属性绑定到一个布尔值,如下所示:

<TabControl>
    <TabControl.Style>
        <Style TargetType="TabControl">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsRunning, UpdateSourceTrigger=PropertyChanged}" Value="True">
                    <Setter Property="SelectedIndex" Value="1" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TabControl.Style>
    <TabItem Header="Foo" />
    <TabItem Header="Bar" /> 
</TabControl>

TabControl 应该只切换到第二个选项卡,如果IsRunning 属性更改为 True,但现在的问题是,一旦 IsRunning 属性更改,TabControl 不会更新自身以显示第二个TabItem

有没有办法通过 XAML 来执行此操作,或者我是否必须在视图模型中实现 SelectedIndex 属性,该属性直接绑定到 TabControl< 的 SelectedIndex /代码>?

I have a TabControl which has its SelectedIndex property bound to a boolean value like this:

<TabControl>
    <TabControl.Style>
        <Style TargetType="TabControl">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsRunning, UpdateSourceTrigger=PropertyChanged}" Value="True">
                    <Setter Property="SelectedIndex" Value="1" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TabControl.Style>
    <TabItem Header="Foo" />
    <TabItem Header="Bar" /> 
</TabControl>

The TabControl should only switch to the second tab, if the IsRunningproperty changes to True, but the problem now is, that as soon as the IsRunning property changes, the TabControl does not update itself to display the second TabItem.

Is there a way to do this through XAML, or do I have to implement a SelectedIndex property in my viewmodel, that binds directly to the SelectedIndexof the TabControl?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥有 2025-01-03 18:00:06

如果属性更改为 true 选项卡切换,这对我来说就像预期的那样。也许有一个 绑定有问题? (或者我误解了这个问题?)

This works for me just as expected, if the property changes to true the tab switches. Maybe there's a problem with the binding? (Or did i misuderstand the question?)

唐婉 2025-01-03 18:00:06

这是一个旧线程,但谁知道其他人可能会像我一样偶然发现这个问题并寻找答案。

解决方案:只需在 TabControl 样式中添加一个 setter 将 SelectedIndex 设置为初始值即可。例如 Setter 属性 =“SelectedIndex”值 =“0”

<TabControl>
    <TabControl.Style>
        <Style TargetType="TabControl">
            <Setter Property="SelectedIndex" Value="0" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsRunning, UpdateSourceTrigger=PropertyChanged}" Value="True">
                    <Setter Property="SelectedIndex" Value="1" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TabControl.Style>
    <TabItem Header="Foo" />
    <TabItem Header="Bar" /> 
</TabControl>

This is an old thread but who knows someone else may stuble upon this just like me looking for an answer.

Solution: Just add a setter in TabControl style to set SelectedIndex to initial value. e.g. Setter Property="SelectedIndex" Value="0"

<TabControl>
    <TabControl.Style>
        <Style TargetType="TabControl">
            <Setter Property="SelectedIndex" Value="0" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsRunning, UpdateSourceTrigger=PropertyChanged}" Value="True">
                    <Setter Property="SelectedIndex" Value="1" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TabControl.Style>
    <TabItem Header="Foo" />
    <TabItem Header="Bar" /> 
</TabControl>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文