c# - 单击另一个 DataGrid 时如何失去对 DataGrid 的焦点?

标签 c# wpf mvvm datagrid

我正在使用 MVVM WPF,当我单击另一个 DataGrid 时,我需要失去焦点,因此我将仅选择一个 DataGrid 中的一行。

This is my recent state

或者,如果可能的话,当我在 DataGrids 外部单击时会失去焦点吗?但是,当我想单击 DataGrid 中选定行上的编辑按钮时,它也会失去焦点吗?

protected override void OnLostFocus(System.Windows.RoutedEventArgs e)
{
  base.OnLostFocus(e);
  this.SelectedItems.Clear();
  this.SelectedItem = null;
}

这是我的 OnLostFocus,但我不知道如何在 xaml 中捕捉点击 (我对xaml真的很陌生)

更新:我可以使用样式触发器以某种方式将焦点从一个 DataGrid 转移到另一个 DataGrid 吗?

最佳答案

我在代码后面修复了它。这就像一个魅力! (使用 MouseDown 效果不佳,我不知道为什么)

 public override void OnApplyTemplate()
{
  base.OnApplyTemplate();

  dataGrid = GetTemplateChild("dataGrid") as DataGrid;
  dataGrid.MouseUp += new MouseButtonEventHandler(dataGrid_MouseUp);
  docGrid = GetTemplateChild("docGrid") as DataGrid;
  docGrid.MouseUp += new MouseButtonEventHandler(docGrid_MouseUp);
}

public void dataGrid_MouseUp(object sender, MouseEventArgs e)
{
  docGrid.UnselectAll();
}

public void docGrid_MouseUp(object sender, MouseEventArgs e)
{
  dataGrid.UnselectAll();
}

关于c# - 单击另一个 DataGrid 时如何失去对 DataGrid 的焦点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32742300/

相关文章:

c# - 有一个待处理的异步操作,并且只能同时处理一个异步操作

c# - 运算符 '&&' 不能应用于类型 'System.DateTime' 和 'System.DateTime' 的操作数

c# - WPF 控件未正确刷新

WPF Bing map 控件 - 更改比例尺单位

wcf - 模型 - View - ViewModel & WCF - WCF 是模型吗?

wpf - 如何使用 MVVM 在 ListView 中设置特定项目?

c# - MySQL 连接器转换问题

c# - 如何从配置文件禁用 Entity Framework 中的缓存

c# - DataContext 绑定(bind)和刷新

c# - 在 StackPanel WPF 中使用 MouseOver