2012-06-06 41 views
1

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

回答

1

我可以建議的最好的事情是映射到常規事件處理程序,然後從您的控件中調用OutOfViewCommand.Execute到您的DataContext。您還需要在控件上映射UserControl.DataContextChanged,並在本地保存您的datacontext。

public partial class MainWindow : Window 
{ 
    private object Data { get; set; } 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void Window_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) 
    { 
     this.Data = e.NewValue; 
    } 

    private void Window_Unloaded(object sender, RoutedEventArgs e) 
    { 
     if(this.Data != null) 
      this.Data.OutOfViewCommand.Execute(null); 
    } 
} 

XAML:

<Window x:Class="WpfApplication3.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525" DataContextChanged="Window_DataContextChanged" FrameworkElement.Unloaded="Window_Unloaded"> 
<Grid> 

</Grid> 

雖然這並不嚴格與MVVM符合,你會經常面對框架調用的妥協,它仍然工作在任何一個可重複使用的方式查看模型。

+0

爲了更通用的解決方案,你可以取代「this.Data.OutOfViewCommand .Execute(null)「with」(DataBinder.Eval(this.Data,「OutOfViewCommand」)as ICommand).Execute(null)「 – jimmyjambles

1

爲此,您可能需要正確地命名連接proerty ....它的名字聲明是OutOfViewCommand,但它應該是UnloadedCommand

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; 
    } 
} 
+0

似乎解決了運行時錯誤,但命令未執行。 –

相關問題