DataGrid失去焦点,但SelectedItem未设置为null

时间:2014-03-03 12:55:45

标签: c# wpf datagrid focus selecteditem

我在WPF中有一个DataGrid。我已经绑定了SelectedItem属性。我有删除按钮,删除选定的行。但是,当我单击其他位置(即TextBox)并且DataGrid丢失焦点时,SelectedItem不会更改。我可以删除“早先”选择的行。

是您预期的默认行为吗?可以轻松修复吗?

编辑(代码按要求):

<DataGrid SelectedItem="{Binding SelectedPort}" (...) >

<Button Command="{Binding RemovePortCommand}" >Remove Port</Button>

public ICommand RemovePortCommand
{
    get
    {
        return _removePortCommand ?? (_removePortCommand = new RelayCommand(param => OnRemovePortCommand(), param => SelectedPort != null));
    }
}

1 个答案:

答案 0 :(得分:2)

那是好人......

只需使用您的Binding属性设置

grid.SelectedItem =null;

OR

grid.selectedindex = -1

或两者,删除后

相关问题