为什么我的 DataTrigger 不更新 TabControl 的 SelectedIndex 属性?
我有一个 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 IsRunning
property 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 SelectedIndex
of the TabControl
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果属性更改为 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?)
这是一个旧线程,但谁知道其他人可能会像我一样偶然发现这个问题并寻找答案。
解决方案:只需在 TabControl 样式中添加一个 setter 将 SelectedIndex 设置为初始值即可。例如 Setter 属性 =“SelectedIndex”值 =“0”
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"