dotnet / wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
MIT License
7.03k stars 1.17k forks source link

Datagrid loses its new row placeholder when binded collection is cleared from non-focusable button while user is editing new row. #4032

Open kahko opened 3 years ago

kahko commented 3 years ago

ViewModel:

public class MainVM
  {
    public ObservableCollection<A> Collection { get; } = new ObservableCollection<A>();

    private ICommand _clearCommand;
    public ICommand ClearCommand
    {
        get
        {
            if (_clearCommand is null)
                _clearCommand = new RelayCommand(_ => Collection.Clear(), _ => Collection.Any());
            return _clearCommand;
        }
    }
}

public class A
{
    public string Name { get; set; }
    public int Age { get; set; }
}
  1. Start creating datagridrow: editing
  2. Click the unfocusable button without finishing started new row: result

    Expected behavior: Datagrid does not lose its new row placeholder and user is able to add new rows.

    Minimal repro: https://github.com/kahko/WPFDatagridClearBug

Solution has both .NET Core and .NET Framework projects.

daisyTian0517 commented 3 years ago

How about using a Button to add a new item for DataGrid? Or set MoveFocus DataGrid to add a new Item for DataGrid like below:

private void myGrid_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            int i = myGrid.Items.Count - 1;
            if(i==1)
            {
                myGrid.SelectedIndex = 0;
                DataGridRow row = (DataGridRow)myGrid.ItemContainerGenerator.ContainerFromIndex(1);
                row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
            }
        }
kahko commented 3 years ago

@daisyTian0517 Thanks for the reply, but I'm not looking for workarounds - just reporting what I think is a bug.