我使用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