在WPF中将事件绑定到属性,反之亦然 [英] Bind events to properties and vice-versa in WPF

查看:71
本文介绍了在WPF中将事件绑定到属性,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对绑定和WPF还是比较陌生,但是我已经能够用绑定做一些有趣的事情(至少对我感兴趣)

I am rather new to bindings and WPF, but I've been able to do a few interesting things with bindings (interesting to me at least)

anyoine是否知道是否可以将事件(例如SelectionChanged o Click事件等)绑定到属性(例如IsChecked,IsEnabled等)?

Does anyoine know if it's possible to bind an event (For example a SelectionChanged o Click event, etc) to a property (For example: IsChecked, IsEnabled, etc)?

我知道我可以将Path属性和所有属性绑定在一起.

I know I can bind properties with the Path property and all.

推荐答案

如果要将事件绑定到Command,则可以通过附加行为"来完成.看看此博客关于如何使用MVVM Light进行操作.绑定到 IsEnabled 没有多大意义

If you want to bind events to Commands you can do it through Attached Behavours. Have a look at this blog as to how to do that with MVVM Light. Binding to IsEnabled does not make much sense

他们提供的示例是:

<Button>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click" >
            <i:InvokeCommandAction Command="{Binding FooCommand}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>

在ViewModel中

In ViewModel

public MyViewModel()
{
    //set it as a toggle for example
    FooCommand = new RelayCommand( () => IsChecked = !IsChecked );
 }    

public ICommand FooCommand { get; private set; }

public bool  IsChecked
{
   get { return _isChecked; }
   set { _isChecked = value;
        RaisePropertyChanged("IsChecked"); }
}

这篇关于在WPF中将事件绑定到属性,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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