WPF DataGrid CellEditEnding - DataSet Not Updating Till Row Lost Focus

后端 未结 5 903
粉色の甜心
粉色の甜心 2020-12-16 14:59

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

相关标签:
5条回答
  • 2020-12-16 15:16

    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;
        }
    
    0 讨论(0)
  • 2020-12-16 15:24

    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);
    
    0 讨论(0)
  • 2020-12-16 15:26

    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.

    0 讨论(0)
  • 2020-12-16 15:30

    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.

    0 讨论(0)
  • 2020-12-16 15:34

    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
    0 讨论(0)
提交回复
热议问题