WPF绑定FrameworkElement事件到命令 [英] WPF Binding FrameworkElement event to command

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

问题描述

如何将 UserControl FrameworkElement 事件绑定到视图模型命令?我使用MVVM和Prism这样清晰的视图之间的分离视图模型会很好。



我尝试过多个东西,没有一个工作:

 < i:Interaction.Triggers> 
< i:EventTrigger EventName =FrameworkElement.Unloaded>
< i:InvokeCommandAction Command ={Binding Unloaded}/>
< / i:EventTrigger>
< / i:Interaction.Triggers>

还使用本教程 http://blog.functionalfun.net/2008/09/hooking-up-commands-to-events-in- wpf.html

  local:FrameworkElementBehavior.UnloadedCommand ={Binding Unloaded}

我别无选择,只能为我的代码添加一些功能。



以下是我的查看模式:



公共类CustomerViewModel:PosViewModelBase
{
public ICommand Unloaded
{
get {return new UnloadedCommand();
}

public CustomerViewModel()
{

}

私有类UnloadedCommand:ICommand
{
public bool CanExecute(object parameter)
{
return true;
}

public event EventHandler CanExecuteChanged;

public void Execute(object parameter)
{
Debug.WriteLine(Customer stuff out out of view);
}
}
}


解决方案

我认为问题可能在卸载事件。



从MSDN页面 http://msdn.microsoft.com/en-us/library/ms754221.aspx#common_events



卸载是最后提出的,由演示源或视觉父代被删除。当Unloaded被提升和处理时,在逻辑或视觉树中的事件源父元素(由Parent属性确定)或任何给定元素向上的元素可能已经被取消设置,这意味着数据绑定,资源引用和样式可能不能设置为其正常或最后知道的运行时值。


How can I bind a UserControl's FrameworkElement event to a view model command? I use MVVM and Prism so clear separation between the view the view-model would be nice.

I'd tried multiple things, and none of them worked:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="FrameworkElement.Unloaded">
        <i:InvokeCommandAction Command="{Binding Unloaded}" />
    </i:EventTrigger>
</i:Interaction.Triggers>

also using this tutorial http://blog.functionalfun.net/2008/09/hooking-up-commands-to-events-in-wpf.html

local:FrameworkElementBehavior.UnloadedCommand="{Binding Unloaded}"

Do I have no choice but to add some functions to my code-behind?

Neither of the attempts above error out, but the command does not execute.

Here's my view model:

public class CustomerViewModel : PosViewModelBase
{
    public ICommand Unloaded
    {
        get { return new UnloadedCommand(); }
    }

    public CustomerViewModel()
    {

    }

    private class UnloadedCommand : ICommand
    {
        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            Debug.WriteLine("Customer stuff is out of view");
        }
    }
}

解决方案

I think the problem could be in Unloaded event.

From MSDN page http://msdn.microsoft.com/en-us/library/ms754221.aspx#common_events:

Unloaded is raised last and is initiated by either the presentation source or the visual parent being removed. When Unloaded is raised and handled, the element that is the event source parent (as determined by Parent property) or any given element upwards in the logical or visual trees may have already been unset, meaning that data binding, resource references, and styles may not be set to their normal or last known run-time value.

这篇关于WPF绑定FrameworkElement事件到命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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