【问题标题】:MVVM View event Viewmodel command bindingMVVM View 事件 Viewmodel 命令绑定
【发布时间】:2012-06-17 07:52:15
【问题描述】:

我正在寻找一个关于如何使用 Prism 实现事件聚合器的好(阅读:简单)示例。我从未使用过 Prism,而且我对 MVVM 本身也很陌生。

我有一个 WPF 画布作为视图,我想在 Viewmodel 的画布上处理 MouseUp 事件。现在我们组织的权力要我使用 Prism,显然 Prism 建议使用事件聚合器,这就是为什么我需要一个样本来帮助我开始。

【问题讨论】:

    标签: mvvm prism viewmodel commandbinding


    【解决方案1】:

    AttachedCommandBehavior V2 aka ACB 提出了一种更通用的使用行为方式,它甚至支持多个事件到命令的绑定,

    这是一个非常基本的使用示例:

    <Border local:CommandBehavior.Event="MouseDown"  
            local:CommandBehavior.Command="{Binding DoSomething}"
            local:CommandBehavior.CommandParameter="From the DarkSalmon Border"
    />
    

    【讨论】:

      【解决方案2】:

      我不知道 PRISM 的 EventAggregator 允许事件->命令绑定。

      在这种情况下,您的另一个选择是使用“行为”。这是一个不错的概述行为:http://wpftutorial.net/Behaviors.html。您可以忽略本教程的 Blend 部分;重要的是您至少安装了 Blend 3 SDK。我是这样做的:

      public class ButtonDoubleClickCommandBehavior : Behavior<Button>
      {
          public ICommand DoubleClickCommand
          {
              get { return (ICommand)GetValue(DoubleClickCommandProperty); }
              set { SetValue(DoubleClickCommandProperty, value); }
          }
      
          public static readonly DependencyProperty DoubleClickCommandProperty =
              DependencyProperty.Register("DoubleClickCommand", typeof(ICommand), typeof(ButtonDoubleClickCommandBehavior));
      
          protected override void OnAttached()
          {
              this.AssociatedObject.MouseDoubleClick += AssociatedObject_MouseDoubleClick;
          }
      
          protected override void OnDetaching()
          {
              if (this.AssociatedObject != null)
              {
                  this.AssociatedObject.MouseDoubleClick -= AssociatedObject_MouseDoubleClick;
              }
          }
      
          void AssociatedObject_MouseDoubleClick(object sender, MouseButtonEventArgs e)
          {
              if (DoubleClickCommand != null && DoubleClickCommand.CanExecute(null))
              {
                  DoubleClickCommand.Execute(null);
              }
          }
      }
      

      您可以将另一个依赖属性添加到行为以绑定命令参数,以便您可以使用该参数执行命令;我只是在我的示例中使用了 null。

      还有我的 XAML:

      <Button Content="{Binding Path=Description}" VerticalAlignment="Center" HorizontalAlignment="Stretch" Template="{StaticResource TextBlockButtonTemplate}" Style="{StaticResource ZynCommandButton}" Tag="DescriptionButton">
          <e:Interaction.Behaviors>
              <ZViewModels:ButtonDoubleClickCommandBehavior DoubleClickCommand="{Binding Path=ItemDescriptionCommand}"/>
          </e:Interaction.Behaviors>
      </Button>
      

      【讨论】:

        【解决方案3】:

        您只需要来自 MVVMLight 或 System.Windows.Interactivity (Blend SDK) 的 EventToCommand 行为。我建议您使用 MVVMLight 版本,因为它有一些有用的特色:)

        <Canvas>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseUp" >
                <i:InvokeCommandAction Command="{Binding YourMouseUpViewModelCommand}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
        </Canvas>
        

        来自 Prism 的 EventAggregator 我主要用于解耦 Viewmodel 到 Viewmodel 的通信。

        【讨论】:

          猜你喜欢
          • 2011-11-07
          • 1970-01-01
          • 2013-08-29
          • 1970-01-01
          • 1970-01-01
          • 2014-11-06
          • 2011-12-14
          • 2018-09-20
          • 2010-12-23
          相关资源
          最近更新 更多