【发布时间】:2020-02-27 05:18:52
【问题描述】:
我在 WPF 中有一个 DataGrid。
我有一种情况,如果选择了一行,然后用户选择了另一行,那么应该有一条消息,例如 -
您确定要选择这个吗?
如果用户说是,那么只有另一行应该被选中。
但是,在当前情况下,SelectionChanged 事件已经被调用并且新行被选中。
我尝试的是使用一些属性绑定
<Style TargetType="DataGridRow">
<Setter Property="IsEnabled" Value="{Binding IsToEnableRowSelection, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"/>
</Style>
在后端 -
private void MyDataGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (MessageBox.Show("Are you sure you want save changes?", "Confirm", MessageBoxButtons.YesNo))
{
e.Handled = false;
IsToEnableRowSelection = true;
}
else
{
e.Handled = true;
IsToEnableRowSelection = false;
}
}
带有 INotify 事件的属性声明 -
public bool IsToEnableRowSelection
{
get
{
return enableSelectedRow;
}
set
{
enableSelectedRow = value;
OnPropertyChanged("IsToEnableRowSelection");
}
}
protected void OnPropertyChanged([CallerMemberName()] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
但我无法获得所需的行为。
所以,总而言之,我希望只有在 用户确认它在消息框中被选中。其他旧选择 行只应保持选中状态。
【问题讨论】:
-
您可以在这里找到解决方案:How can I override DataGrid selection behavior?
-
@Rekshino - 让我检查一下。会让你知道的。