WPF 数据网格鼠标绑定左双击命令仅在已选择项时才有效



我有一个DataGrid,并希望在双击一行时执行在我的ViewModel中定义的命令。这在行已被选中时有效,但如果我双击当前未选择的行,则不起作用,即使我可以看到 DataGrid 的 MouseDoubleClick 事件正在触发并且所需的行已成为 SelectedItem。以下是我的数据网格的定义:

<DataGrid Name="ProjectsList" Grid.Row="1" ItemsSource="{Binding FilteredProjects}" VerticalAlignment="Stretch" 
HorizontalAlignment="Stretch" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False"
SelectedItem="{Binding SelectedProject}" PreviewMouseDoubleClick="ProjectsList_PreviewMouseDoubleClick" 
MouseDoubleClick="ProjectsList_MouseDoubleClick" >
<interactivity:Interaction.Behaviors>
<behaviours:DataGridSelectedProjectsBehaviour SelectedProjects="{Binding SelectedProjects}" />
</interactivity:Interaction.Behaviors>
<DataGrid.InputBindings>
<KeyBinding Key="Delete" Command="{Binding DeleteCommand}" />
<MouseBinding MouseAction="LeftDoubleClick" Command="{Binding OpenCommand}" />
</DataGrid.InputBindings>
<DataGrid.Columns>
<DataGridTemplateColumn Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid Margin="0,0,8,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Rectangle Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Height="2">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
<GradientStop Color="{StaticResource SpruceGreen}" Offset="0" />
<GradientStop Color="{StaticResource DarkGrey}" Offset="0.5" />
<GradientStop Color="{StaticResource LightGrey}" Offset="1" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<TextBlock Grid.Column="0" Grid.Row="1" Text="{Binding Name}" FontWeight="Bold" />
<TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding State}" Foreground="Red" HorizontalAlignment="Right" Typography.Capitals="AllSmallCaps" />
<TextBlock Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" Text="{Binding Created, Converter={StaticResource StringFormat}, ConverterParameter='CREATED {0:dd/MM/yyyy HH:mm}'}" Foreground="Gray" FontSize="10" />
<TextBlock Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="2" Text="{Binding Description}" TextWrapping="Wrap" Padding="0,0,0,32" />
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>

下面是一些演示问题的日志条目。请原谅我的调试日志条目速记。以> 结尾的日志条目仅指示调用了该方法([] 中的名称(。 当我双击当前未选择的行时,就会发生这种情况:

2017/07/12 16:35:52 DEBUG (ThreadId:1) [DataGridSelectedProjectsBehaviour.OnDataGridSelectionChanged] - > 
2017/07/12 16:35:52 DEBUG (ThreadId:1) [SelectProjectViewModel.SelectedCollectionChanged] - > 
2017/07/12 16:35:52 DEBUG (ThreadId:1) [SelectProjectViewModel.get_SelectedTotal] - > 
2017/07/12 16:35:52 DEBUG (ThreadId:1) [DataGridSelectedProjectsBehaviour.ProjectsCollectionChanged] - > 
2017/07/12 16:35:52 DEBUG (ThreadId:1) [SelectProjectViewModel.SelectedCollectionChanged] - > 
2017/07/12 16:35:52 DEBUG (ThreadId:1) [SelectProjectViewModel.get_SelectedTotal] - > 
2017/07/12 16:35:52 DEBUG (ThreadId:1) [DataGridSelectedProjectsBehaviour.ProjectsCollectionChanged] - > 
2017/07/12 16:35:52 DEBUG (ThreadId:1) [SelectProjectPage.ProjectsList_PreviewMouseDoubleClick] - > 
2017/07/12 16:35:52 DEBUG (ThreadId:1) [SelectProjectPage.ProjectsList_PreviewMouseDoubleClick] - SelectedItem is project [Project2] 
2017/07/12 16:35:52 DEBUG (ThreadId:1) [SelectProjectPage.ProjectsList_MouseDoubleClick] - > 
2017/07/12 16:35:52 DEBUG (ThreadId:1) [SelectProjectPage.ProjectsList_MouseDoubleClick] - SelectedItem is project [Project2] 

当我双击当前选中的行时,就会发生这种情况:

2017/07/12 16:35:54 DEBUG (ThreadId:1) [SelectProjectPage.ProjectsList_PreviewMouseDoubleClick] - > 
2017/07/12 16:35:54 DEBUG (ThreadId:1) [SelectProjectPage.ProjectsList_PreviewMouseDoubleClick] - SelectedItem is project [Project2] 
2017/07/12 16:35:54 DEBUG (ThreadId:1) [<Open>d__35.MoveNext] - command was invoked. 
2017/07/12 16:35:54  INFO (ThreadId:1) [BaseViewModel.CheckForUnsavedChanges] - There are no unsaved changes. 
2017/07/12 16:35:54 DEBUG (ThreadId:1) [ProjectSummaryViewModel..ctor] - ViewModel was created. 
2017/07/12 16:35:54 DEBUG (ThreadId:1) [NavigationServiceEx.InternalNavigateTo] - SetParameter finished 
2017/07/12 16:35:54 DEBUG (ThreadId:1) [BaseViewModel.NavigatedFrom] - > 
2017/07/12 16:35:54 DEBUG (ThreadId:1) [SelectProjectPage.ProjectsList_MouseDoubleClick] - > 
2017/07/12 16:35:54 DEBUG (ThreadId:1) [SelectProjectPage.ProjectsList_MouseDoubleClick] - SelectedItem is project [Project2] 

如您所见,如果已选择该行,则会执行"打开"命令。

我曾认为 DataGridSelectedProjectsBehavior 可能会干扰 MouseBinding,但是当我删除它时,双击命令永远不起作用。我真的不需要它,想删除它。当预期需要多行选择时,添加了它。

任何建议将不胜感激。谢谢

您可以尝试处理DataGridRow容器的MouseDoubleClick事件,方法是从视图的代码隐藏中调用命令:

<DataGrid Name="ProjectsList" Grid.Row="1" ItemsSource="{Binding FilteredProjects}" VerticalAlignment="Stretch" 
HorizontalAlignment="Stretch" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False"
SelectedItem="{Binding SelectedProject}" PreviewMouseDoubleClick="ProjectsList_PreviewMouseDoubleClick" 
MouseDoubleClick="ProjectsList_MouseDoubleClick" >
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<EventSetter Event="MouseDoubleClick" Handler="OnItemMouseDoubleClick" />
</Style>
</DataGrid.RowStyle>
...
</DataGrid>

private void OnItemMouseDoubleClick(object sender, ouseButtonEventArgs e)
{
var vm = this.DataContext as YourViewModel;
vm.YourCommand.Execute(null);
}

或者通过使用附加行为并基本上做同样的事情:

<Style TargetType="DataGridRow">
<Setter Property="local:YourType.YourAttachedCommandProperty" Value="{Binding DataContext.YourCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
</Style>

最新更新