如何绑定在MVVM mousedouble点击命令 [英] How to bind mousedouble click command in mvvm

查看:130
本文介绍了如何绑定在MVVM mousedouble点击命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ListView和我想有显示新窗口中的任何位置,当有人双击。但我有MVVM应用程序,我不希望有任何代码的功能XAML文件的背后,是这样的:的如何绑定命令在DataGrid中和许多其他样本像这样的行双击。我想在视图模型文件的方法,并将其绑定是这样的:

I have listview and I want to have show new window when someone double click in any position. But I have mvvm application and I don't want to have any function in code behind of xaml file, like this: How to bind a Command to double-click on a row in DataGrid and many other samples like this. I want to have method in viewmodel file and bind it like this:

<ListView ... MouseDoubleClick="{Binding myfunction}"> 



感谢

Thanks

推荐答案

这是基于在列表中单击项目来触发命令(在视图模型)的方法的工作示例。在视图模型的命令将获得点击项目作为其参数。

This is a working example of a method to trigger a command (In the ViewModel) based on the clicked item in a list. The command in the ViewModel will get the "clicked" item as its parameter.

我使用的Textblock.InputBindings而且可能是的混合的一部分SDK 的由Blachshma联系,但你不需要任何其他DLL为此工作。

I'm using the Textblock.InputBindings and that might be part of the Blend SDK linked by Blachshma, but you will not need any other DLLs for this to work.

在我的例子视图模型绑定到用户控件的DataContext的,这就是为什么我需要使用的的RelativeSource FindAncestor 的从我的TextBlock找到视图模型

In my example the ViewModel is bound to the DataContext of the UserControl, that is why I need to use the RelativeSource FindAncestor to find the ViewModel from my TextBlock.

修改
通过绑定的宽度的的的的TextBlock 的到的的ListBox 的的的 ActualWidth的的固定宽度的问题。

Edit: Fixed the width problem by binding the Width of the TextBlock to the ActualWidth of the ListBox.

只是有一个问题,双击将只有当你点击TextBlock中的文本,即使该列表本身内工作更广泛的线

Just one problem, the double click will only work when you click inside the text in the textblock even if the list itself is much wider.

    <ListView ItemsSource="{Binding Model.TablesView}"   Grid.Row="1" 
              SelectedItem="{Binding Model.SelectedTable, Mode=TwoWay}"  >
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path=.}" 
                   Width="{Binding Path=ActualWidth, 
                             RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}" >
                    <TextBlock.InputBindings>
                        <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding DataContext.MoveItemRightCommand,
                                        RelativeSource={RelativeSource FindAncestor, 
                                        AncestorType={x:Type UserControl}}}"
                                      CommandParameter="{Binding .}"/>
                    </TextBlock.InputBindings>
                </TextBlock>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

这篇关于如何绑定在MVVM mousedouble点击命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆