使用 MVVM 模式处理控件上的鼠标事件 - 最佳实践 -

发布于 2024-09-06 05:27:46 字数 1253 浏览 1 评论 0原文

我实际上发现了两种使用 mvvm 模式处理控件上鼠标事件的方法。

两种方式实际上都是一种方式:

MVVM Light Toolkit by http://mvvmlight.codeplex.com/

<i:Interaction.Triggers>
    <i:EventTrigger EventName="SelectionChanged">
        <cmd:EventToCommand
            Command="{Binding SelectionChangedCommand}"
            CommandParameter="{Binding SelectedItems,
                ElementName=MyDataGrid}" />
    </i:EventTrigger>
</i:Interaction.Triggers>

和 Blend interactivity.dll 与 Behaviours

<i:Interaction.Triggers>
  <i:EventTrigger EventName=”MouseLeftButtonDown”>
    <Behaviours:ExecuteCommandAction Command=”{Binding MyCommand}” CommandParameter=”{Binding MyCommandParameter}”/>
  </i:EventTrigger>
</i:Interaction.Triggers>

你知道有更好的方法吗?

主持人:为什么我的最后 6 行 xaml 代码根本不可见? 它们被IE和Iron浏览器吞没了。 您能否报告管理员修复该代码脚本?它经常不工作。证明: http://img251.imageshack.us/img251/5236/errorxt.png

I found actually 2 ways to handle mouse events on controls with the mvvm pattern.

Both ways are actually 1 way:

MVVM Light Toolkit by http://mvvmlight.codeplex.com/

<i:Interaction.Triggers>
    <i:EventTrigger EventName="SelectionChanged">
        <cmd:EventToCommand
            Command="{Binding SelectionChangedCommand}"
            CommandParameter="{Binding SelectedItems,
                ElementName=MyDataGrid}" />
    </i:EventTrigger>
</i:Interaction.Triggers>

and the Blend interactivity.dll with Behaviours

<i:Interaction.Triggers>
  <i:EventTrigger EventName=”MouseLeftButtonDown”>
    <Behaviours:ExecuteCommandAction Command=”{Binding MyCommand}” CommandParameter=”{Binding MyCommandParameter}”/>
  </i:EventTrigger>
</i:Interaction.Triggers>

Do you know of any better method?

Moderator: Why the heck are my last 6 xaml lines of code not visible at all?
They are swallowed by IE and Iron browser.
Would you please report the admin to fix that code script? its not working at all very often. prove: http://img251.imageshack.us/img251/5236/errorxt.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屋顶上的小猫咪 2024-09-13 05:27:46

如果您需要在任意位置处理 MouseDown,这些都是很好的方法。

然而,这些情况通常很少见。通常有一个更简单的方法:

  • 您确定您的对象不是真正的按钮,只是看起来不像按钮?如果是这样,请将它们设为真正的 Button 对象并对其进行模板化,使其看起来像您想要的那样。
  • 您确定您的对象只是列表中对象的选择区域吗?如果是这样,请将容器从 ItemsControl 更改为 ListBox 并重新设置 ListBoxItem 的样式以使用选择区域。
  • 您的对象是否被选择图形路径?使用内容为路径本身的切换按钮。

这方面还有很多其他例子。事实上,将 MouseDown 映射到 Command 的情况并不常见,并且没有更简洁的方法来完成相同的操作。

Those are both good ways to do it if you need to handle MouseDown in arbitrary places.

However these situations generally are few and far between. Usually there is a simpler way:

  • Are you sure your objects aren't really buttons that just don't look like buttons? If so, make them real Button objects and template them to look the way you want.
  • Are you sure your objects are just selection areas for objects in a list? If so, change the container from ItemsControl to ListBox and restyle ListBoxItem to use the selection areas.
  • Are your objects graphical paths that are being selected? Use a ToggleButton whose content is the path itself.

There are many other examples of this. In fact, it is uncommon to find a situation in which a MouseDown maps to a Command and there isn't a cleaner way to do the same thing.

站稳脚跟 2024-09-13 05:27:46

总有另一种选择。您可以在 View 的代码隐藏中处理 WPF 事件,并在 ViewModel 上调用适当的方法。 MVVM模式并不禁止在View的代码隐藏文件中写入任何代码。

WPF 应用程序框架 (WAF)ViewModel 示例应用程序> 展示了这是如何工作的。

There is always another option. You can handle WPF events in the code-behind of the View and call the appropriate method on the ViewModel. The MVVM pattern doesn't forbid to write any code in the code-behind file of the View.

The ViewModel sample application of the WPF Application Framework (WAF) shows how this can work.

痴梦一场 2024-09-13 05:27:46

XCommand 开源 codeplex 项目有更好的方法来处理基于 Command/CommandParameter 绑定的事件。在这里找到,xcommand.codeplex.com

下面是示例代码:

<Grid>
    <TextBlock Margin="20,30,20,0" VerticalAlignment="Top" Height="80" x:Name="XTextBlock"
           Foreground="{Binding FgColor, Mode=TwoWay}"
           XCmd:MouseMove.Command="{Binding TextBlockPointerMovedCommand}"
           XCmd:MouseLeftButtonDown.Command="{Binding TextBlockPointerPressedCommand}"
           XCmd:MouseLeave.Command="{Binding TextBlockPointerExitedCommand}"    
           Text="{Binding Description, Mode=TwoWay}">
    </TextBlock>
    <Grid Grid.Column="1" Background="{Binding BgColor, Mode=TwoWay}"
          XCmd:MouseMove.Command="{Binding GridPointerMovedCommand}" 
          XCmd:MouseMove.CommandParameter="{Binding ElementName=XTextBlock, Path=Text}"
          XCmd:MouseLeftButtonDown.Command="{Binding GridPointerPressedCommand}"
          XCmd:MouseLeftButtonDown.CommandParameter="{Binding ElementName=XTextBlock, Path=Text}"
          >
    </Grid>
</Grid>

希望这会有所帮助。

XCommand Open source codeplex project has better way to deal with this event based Command/CommandParameter binding. Find here, xcommand.codeplex.com

Here is the sample code below:

<Grid>
    <TextBlock Margin="20,30,20,0" VerticalAlignment="Top" Height="80" x:Name="XTextBlock"
           Foreground="{Binding FgColor, Mode=TwoWay}"
           XCmd:MouseMove.Command="{Binding TextBlockPointerMovedCommand}"
           XCmd:MouseLeftButtonDown.Command="{Binding TextBlockPointerPressedCommand}"
           XCmd:MouseLeave.Command="{Binding TextBlockPointerExitedCommand}"    
           Text="{Binding Description, Mode=TwoWay}">
    </TextBlock>
    <Grid Grid.Column="1" Background="{Binding BgColor, Mode=TwoWay}"
          XCmd:MouseMove.Command="{Binding GridPointerMovedCommand}" 
          XCmd:MouseMove.CommandParameter="{Binding ElementName=XTextBlock, Path=Text}"
          XCmd:MouseLeftButtonDown.Command="{Binding GridPointerPressedCommand}"
          XCmd:MouseLeftButtonDown.CommandParameter="{Binding ElementName=XTextBlock, Path=Text}"
          >
    </Grid>
</Grid>

Hope this will be helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文