2012-06-06 188 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

似乎解决了运行时错误,但命令未执行。 –