如何在触发器上获取 TabHeader
我有一个选项卡控件。我试图将 tabcontrol 作为参数传递来找出所选的选项卡项目,以便我可以获得选项卡标题名称。绑定这个好像不行想法?
<TabControl Background="#FFF9F9F9" Height="650">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<n:ExecuteCommandAction Command="{Binding UpdateTabCommand}" Parameter="{Binding this}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
在我的视图模型构造函数中,我有:
_updateTabCommand = new ActionCommand< TabControl>(UpdateTab);
私有方法:
public void UpdateTab(TabControl tabControl)
{
var tabItem = (TabItem)tabControl.SelectedItem;
i have a tabcontrol. i'm trying to pass the tabcontrol as a parameter to figure out the selected tab item so i can get the tab header name. Binding this doesn't seem to work. ideas?
<TabControl Background="#FFF9F9F9" Height="650">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<n:ExecuteCommandAction Command="{Binding UpdateTabCommand}" Parameter="{Binding this}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
in my viewmodel constructor i have:
_updateTabCommand = new ActionCommand< TabControl>(UpdateTab);
private method:
public void UpdateTab(TabControl tabControl)
{
var tabItem = (TabItem)tabControl.SelectedItem;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1) 使用
ElementName
绑定。示例:
2) 使用
FindAncestor
绑定:示例:
1) Use
ElementName
binding.Example:
2) Use
FindAncestor
binding:Example:
首先,WPF 中不存在
{Binding this}
这样的东西。如果要引用要设置绑定的元素,请使用 RelativeSource 绑定,模式设置为 自我。第二次观察。将 UI 元素传递给 ViewModel 的味道很糟糕(影响可测试性,增加代码耦合,并且很可能该类最终会违反更多 良好的设计原则)。修复方法非常简单:只需将 TabControl.SelectedItem 绑定到 ViewModel 上的字段即可。
First of all there is no such thing as
{Binding this}
in WPF. If you want to refer to the element on which you are setting Binding use RelativeSource binding with mode set to Self.Second observation. Passing UI elements to ViewModel smells badly (impacts testability, increases code coupling and most likely the class will end up violating more good design principles). The fix is really simple: just bind
TabControl.SelectedItem
to the field on ViewModel.