使用带有TabItem的命令 [英] Use a Command with TabItem

查看:506
本文介绍了使用带有TabItem的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在选择我的TabControl的TabItem的调用命令。

I would like to call a Command when a TabItem of my TabControl is selected.

有没有办法做到这一点不打破MVVM模式?

Is there a way to do it without breaking the MVVM pattern ?

推荐答案

使用的 AttachedCommand行为,这将让你命令绑定到WPF事件

Use an AttachedCommand Behavior, which will let you bind a Command to WPF events

<TabControl ...
    local:CommandBehavior.Event="SelectionChanged"  
    local:CommandBehavior.Command="{Binding TabChangedCommand}" />



当然,如果你使用MVVM设计模式,结合的SelectedItem 的SelectedIndex ,也可以运行在的PropertyChanged 事件的命令

Of course, if you're using the MVVM design pattern and binding SelectedItem or SelectedIndex, you could also run the command in the PropertyChanged event

void MyViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "SelectedIndex")
        RunTabChangedLogic();
}

这篇关于使用带有TabItem的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆