WPF DataGrid:命令绑定到双击而不是使用事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3876662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF DataGrid: CommandBinding to a double click instead of using Events
提问by m-y
I know how to use the MouseDoubleClick event with my DataGrid to grab the selectedvalue, but how would one go about using command bindings instead? That way my ViewModel can handle the logic.
我知道如何将 MouseDoubleClick 事件与我的 DataGrid 一起使用来获取 selectedvalue,但是如何使用命令绑定呢?这样我的 ViewModel 就可以处理逻辑。
So far I have the following:
到目前为止,我有以下几点:
<DataGrid Name="TestGrid" Grid.Row="2" Grid.ColumnSpan="2" AutoGenerateColumns="True" MouseDoubleClick="TestGrid_MouseDoubleClick"
ItemsSource="{Binding Registrations}" SelectedValue="{Binding CurrentRegistration}" IsReadOnly="True" AlternationCount="2" GridLinesVisibility="None">
I want to get rid of MouseDoubleClick and replace it appropriately.
我想摆脱 MouseDoubleClick 并适当替换它。
回答by Mizipzor
No need for attached behaviors or custom DataGrid subclasses here.
此处无需附加行为或自定义 DataGrid 子类。
In your DataGrid
, bind ItemsSource
to an ICollectionView
. The trick here is to set IsSynchronizedWithCurrentItem="True"
which means the selected row will be the current item.
在您的 中DataGrid
,绑定ItemsSource
到ICollectionView
. 这里的技巧是设置IsSynchronizedWithCurrentItem="True"
这意味着所选行将是当前项目。
The second part of the trick is to bind CommandParameter
to the current item with the forward slash syntax.
技巧的第二部分是CommandParameter
使用正斜杠语法绑定到当前项。
When a row is double clicked, the command will be executed with the clicked row as argument.
当一行被双击时,该命令将以被单击的行作为参数执行。
<DataGrid
ItemsSource="{Binding CollectionView}"
IsSynchronizedWithCurrentItem="True">
<DataGrid.InputBindings>
<MouseBinding
MouseAction="LeftDoubleClick"
Command="{Binding DoubleClickCommand}"
CommandParameter="{Binding CollectionView/}"/>
</DataGrid.InputBindings>
</DataGrid>
This is how a (simplified) version of the view model would look:
这是视图模型的(简化)版本的外观:
class MyViewModel
{
public ICollectionView CollectionView { get; set; }
public ICommand DoubleClickCommand { get; set; }
}
回答by Tamar Cohen
Another solution is to add input bindings, and to bind the selectedItem to a property so you'll know which one was selected:
另一种解决方案是添加输入绑定,并将 selectedItem 绑定到一个属性,以便您知道选择了哪个:
<DataGrid SelectedItem="{Binding SelectedItem}">
<DataGrid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding SomeCommand}"/>
</DataGrid.InputBindings>
</DataGrid>
回答by vortexwolf
Use this library
使用这个库
Sample binding to datagrid event:
绑定到数据网格事件的示例:
<DataGrid xmlns:command="clr-namespace:AttachedCommandBehavior;assembly=AttachedCommandBehavior"
command:CommandBehavior.Event="MouseDoubleClick"
command:CommandBehavior.Command="{Binding TestCommand}" />
But this code is better, because raises on row clicks only:
但此代码更好,因为仅在行点击时引发:
<DataGrid>
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<Setter Property="command:CommandBehavior.Event" Value="MouseDoubleClick"/>
<Setter Property="command:CommandBehavior.Command" Value="{Binding DataContext.TestCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}}"/>
</Style>
</DataGrid.Resources>
</DataGrid>
回答by Catalin Pop
Or, you could create a derived class
或者,您可以创建一个派生类
public class CustomDataGrid : DataGrid
{
public ICommand DoubleClickCommand
{
get { return (ICommand)GetValue(DoubleClickCommandProperty); }
set { SetValue(DoubleClickCommandProperty, value); }
}
// Using a DependencyProperty as the backing store for DoubleClickCommand. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DoubleClickCommandProperty =
DependencyProperty.Register("DoubleClickCommand", typeof(ICommand), typeof(CustomDataGrid), new UIPropertyMetadata());
public CustomDataGrid()
: base()
{
this.PreviewMouseDoubleClick += new MouseButtonEventHandler(CustomDataGrid_PreviewMouseDoubleClick);
}
void CustomDataGrid_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (DoubleClickCommand != null)
{
DoubleClickCommand.Execute(null);
}
}
}
and in XAML simply bind to the newly created command
并在 XAML 中简单地绑定到新创建的命令
<CustomDataGrid DoubleClickCommand="{Binding DoubleClickCommand}">