我使用MVVM模式,所以我的视图模型不知道任何关于该视图,并通过DataTemplates显示视图。WPF将一个ICommand绑定到一个事件(FrameworkElement.Unloaded)
当视图不再显示时,我想截取它(使用实用程序类)。所以我想绑定到FrameworkElement.Unloaded,当它的命中时,截取一个用户控件的截图,以便在另一个控件中使用,以选择要使用哪个视图。
我看了这篇文章,这使得它看起来好像附加属性将工作(我使用它的用户控件对象) http://blog.functionalfun.net/2008/09/hooking-up-commands-to-events-in-wpf.html
我得到的错误只能在DependencyObject的设置绑定或DependencyProperty。我遵循他的指示正确。任何想法为什么这不工作,或者我怎么可以绑定到MVVM场景?
是不是可以绑定到该特定事件或根xaml节点中的事件?
这里的TEH代码(除EventBehaviorFactory在上面的链接)
public static class FrameworkElementBehavior
{
public static readonly DependencyProperty UnloadedCommandProperty = EventBehaviourFactory.CreateCommandExecutionEventBehaviour(FrameworkElement.UnloadedEvent, "UnloadedCommand", typeof(FrameworkElementBehavior));
public static void SetUnloadedCommand(DependencyObject o, ICommand value)
{
o.SetValue(UnloadedCommandProperty, value);
}
public static ICommand GetUnloadedCommand(DependencyObject o)
{
return o.GetValue(UnloadedCommandProperty) as ICommand;
}
}
<UserControl x:Class="WTF.BuxComp.Prism.Modules.Win.OrderEntryPos.Views.CustomerView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WTF.BuxComp.Prism.Modules.Win.OrderEntryPos.Helpers"
mc:Ignorable="d"
d:DesignHeight="510" d:DesignWidth="716"
local:FrameworkElementBehavior.UnloadedCommand="{Binding UnloadedCommand}">
和确切的错误是
A“绑定”不能上的“SetUnloadedCommand”属性设置键入 'CustomerView'。 “绑定”只能在DependencyProperty的DependencyProperty上设置为DependencyObject。
为了更通用的解决方案,你可以取代“this.Data.OutOfViewCommand .Execute(null)“with”(DataBinder.Eval(this.Data,“OutOfViewCommand”)as ICommand).Execute(null)“ – jimmyjambles