WPF DataGrid CellEditEnding - DataSet Not Updating Till Row Lost Focus

怎甘沉沦 提交于 2019-12-29 06:04:12

问题


I need to be able to update values of a dataset once a cell loses focus from editing. I know when the cell loses focus (CellEditEnding), but problem is, the actual updating of it's context item does not occur till focus on that row actually occurs. This becomes a huge issue when there is only one item left, since it may never lose focus.

How do I make sure that each time a column edit is complete (CellEditEnding), the actual context for that row is updated at that point (not just when the row loses focus)

Thanks in advance!


回答1:


I met a similar problem, I have a DataGrid row that contains 5 columns. The data from the 5 columns will be updated in source only after the entire datagrid row has lost focus.

After some search, I found an easy way to do it. That is to add "UpdateSourceTrigger=LostFocus" in your databinding in the cell.

For example:

<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <ComboBox DisplayMemberPath="Name"
                  ItemsSource="{Binding Path=MyDataSets}"
                  SelectedValue="{Binding Path=DataSelected, UpdateSourceTrigger=LostFocus}"/>
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

This will do the trick, so when each of the cell lost focus, instead of the entire row, the data from the cell will update the source immediately.




回答2:


You can use DataGrid.CommitEdit from your DataGrid.CellEditEnding handler, being sure to handle reentrancy.

Here's a blog article that describes the technique:

  • Commiting bound cell changes immediately in WPF Datagrid



回答3:


I had a similar problem and none of the "usual" fixes worked...

What worked for me was to use the overloaded version of CommitEdit() as follows

DataGrid1.CommitEdit(DataGridEditingUnit.Row, true);



回答4:


Simply jump to any other control of your dialog by calling [control].Focus(). Do this inside the OnClosing() event.

The LostFocus is the default update trigger for the data cell. But the window itself and also the border or title of the window (and there the system button "X") cannot get a focus. That's why the editing is not end.




回答5:


You can use the any PreviewMouseMove event from any other object. In my case I want the datagrid to loose control, before adding a new line to it.

MainWindow.xaml.cs

private void MenuItem_PreviewMouseMove(object sender, MouseEventArgs e)
    {
        Grid.CommitEdit();
    }

MainWindow.xaml

 <MenuItem Header="New Line" Command="{Binding CommandNewRow}" PreviewMouseMove="MenuItem_PreviewMouseMove"/>

MainWindowViewModel.cs

    public ICommand CommandNewRow
    {
        get
        {
            if (_commandNewRow == null)
            {
                _commandNewRow = new RelayCommand(p => CommandNewRowExecute(), p => CommandNewRowCanExecute());
            }
            return _commandNewRow;
        }
    }

    private void CommandNewRowExecute()
    {
       FileList.Add(new File("", ""));
    }

    private bool CommandNewRowCanExecute()
    {
        if (FileList.Count > 0)
            return true;
        return false;
    }


来源:https://stackoverflow.com/questions/6246452/wpf-datagrid-celleditending-dataset-not-updating-till-row-lost-focus

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!