使用MVVM设计触发DataGrid单元格编辑事件

时间:2015-03-04 16:14:38

标签: wpf mvvm-light wpfdatagrid

我正在使用Galasoft开发WPF数据网格和MVVM设计。列的几个是可编辑的,我需要在单元格编辑期间执行数据库调用。

我正在尝试在单元格编辑期间捕获事件。我可以使用Galsoft中的EventToCommand选项来完成此操作。但EventToCommand应用于网格级别,并且每个单元格单击事件都会触发该事件。我不想发生这件事。

<DataGrid Grid.Column="0" Grid.Row="1" CanUserResizeRows="False" AutoGenerateColumns="False" x:Name="AdvisorDataGrid" HorizontalScrollBarVisibility="Auto" 
                                        ScrollViewer.VerticalScrollBarVisibility="Auto" IsReadOnly="False" RowHeaderWidth="0" 
                                        Background="White" CanUserSortColumns="True" GridLinesVisibility="All" IsTabStop="False" 
                                         ItemsSource="{Binding MemberAdvisorsList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}"
                                         IsEnabled="{Binding IsGridAdvisorsEnabled}" CanUserAddRows="True" EnableRowVirtualization="True"
                                   CurrentCellChanged="DataGrid_CurrentCellChanged"  SelectedItem="{Binding SelectedMemberDetails,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="OnFocus">
                                    <cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding NavigateToArticleCommand,Mode=OneWay}"/>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>

此外,为了防止每个单元格事件,我将EventToCommand移动到列级别,但随后事件未被触发。

无论如何,我能做到吗?此外,捕获单元格编辑完成操作的最佳事件名称是什么?我目前正在使用LostFocus,但即使是第一次点击也会被解雇。

非常感谢任何帮助。

由于

1 个答案:

答案 0 :(得分:0)

我最后添加了一个条件检查来过滤我需要执行CellEditEnding操作的列。

基本上,OnCellEditEnding事件,我检查Column.Header.equals("Column1")然后执行操作。

默认情况下,所有单元格都会发生此事件。

相关问题