我有一个DataGrid,并希望在我的ViewModel中定义一个命令,当我双击一行时执行。当行已被选中时,这种方法有效,但如果我双击当前未选中的行,则不行,即使我可以看到DataGrid的MouseDoubleClick事件正在触发且所需的行已成为SelectedItem。这是我的DataGrid的定义:
<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>
以下是一些演示此问题的日志条目。请原谅我的Debug日志条目简写。以&gt;结尾的日志条目只是表明调用了方法([]中的名称)。 当我双击当前未选择的行时会发生这种情况:
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]
如您所见,如果已经选择了行,则执行Open命令。
我原以为DataGridSelectedProjectsBehaviour可能会干扰MouseBinding,但当我删除它时,双击命令永远不会起作用。我真的不需要它,并希望删除它。当预期需要多行选择时添加它。
我们非常感谢任何建议。感谢
答案 0 :(得分:1)
您可以尝试通过从视图的代码隐藏调用命令来处理MouseDoubleClick
容器的DataGridRow
事件:
<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>