我有listview,当有人双击任何位置时,我想要显示新窗口.但我有mvvm应用程序,我不希望在xaml文件的代码后面有任何函数,如下所示:如何绑定一个Command来双击DataGrid中的一行以及许多其他样本.我想在viewmodel文件中有方法并将其绑定如下:
<ListView ... MouseDoubleClick="{Binding myfunction}">
Run Code Online (Sandbox Code Playgroud)
谢谢
Pet*_*ell 22
这是根据列表中单击的项触发命令(在ViewModel中)的方法的工作示例.ViewModel中的命令将获取"clicked"项作为其参数.
我正在使用Textblock.InputBindings,它可能是Blachshma链接的Blend SDK的一部分,但您不需要任何其他DLL来实现此功能.
在我的示例中,ViewModel绑定到UserControl的DataContext,这就是我需要使用RelativeSource FindAncestor从我的TextBlock中查找ViewModel的原因.
编辑:通过结合固定宽度问题宽度的的的TextBlock到ActualWidth的所述的列表框.
只有一个问题,双击只有在文本块中的文本内部单击时才会起作用,即使列表本身更宽.
<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>
Run Code Online (Sandbox Code Playgroud)
Gyp*_*psy 15
您可以使用附加属性绑定所需的任何事件.
用于MouseDoubleClick:
namespace Behavior
{
public class MouseDoubleClick
{
public static DependencyProperty CommandProperty =
DependencyProperty.RegisterAttached("Command",
typeof(ICommand),
typeof(MouseDoubleClick),
new UIPropertyMetadata(CommandChanged));
public static DependencyProperty CommandParameterProperty =
DependencyProperty.RegisterAttached("CommandParameter",
typeof(object),
typeof(MouseDoubleClick),
new UIPropertyMetadata(null));
public static void SetCommand(DependencyObject target, ICommand value)
{
target.SetValue(CommandProperty, value);
}
public static void SetCommandParameter(DependencyObject target, object value)
{
target.SetValue(CommandParameterProperty, value);
}
public static object GetCommandParameter(DependencyObject target)
{
return target.GetValue(CommandParameterProperty);
}
private static void CommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
Control control = target as Control;
if (control != null)
{
if ((e.NewValue != null) && (e.OldValue == null))
{
control.MouseDoubleClick += OnMouseDoubleClick;
}
else if ((e.NewValue == null) && (e.OldValue != null))
{
control.MouseDoubleClick -= OnMouseDoubleClick;
}
}
}
private static void OnMouseDoubleClick(object sender, RoutedEventArgs e)
{
Control control = sender as Control;
ICommand command = (ICommand)control.GetValue(CommandProperty);
object commandParameter = control.GetValue(CommandParameterProperty);
command.Execute(commandParameter);
}
}
}
Run Code Online (Sandbox Code Playgroud)
而在Xaml:
<ListBox Behavior:MouseDoubleClick.Command="{Binding ....}"
Behavior:MouseDoubleClick.CommandParameter="{Binding ....}"/>
Run Code Online (Sandbox Code Playgroud)
最简单的方法是使用System.Windows.Interactivity和Microsoft.Expression.Interactions(通过Blend SDK免费提供)
首先,在视图中添加以下命名空间
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
Run Code Online (Sandbox Code Playgroud)
接下来,捕获DoubleClick事件并将其传递给命令:
<ListView ..... >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<local:EventToCommand Command="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DataContext.myfunction}" />
</i:EventTrigger
</i:Interaction.Triggers>
</ListView>
Run Code Online (Sandbox Code Playgroud)
注意:EventToCommand使用的是MVVM Light Toolkit中的一个,可以在这里下载.它的作用是在myFunction触发事件后立即执行command().
这是基于myFunction命令在ListView用户的DataContext中的假设.否则,将EventToCommand的绑定修改为命令所在的位置.