【问题标题】:Where is WPF DataGrid DataBindingComplete event?WPF DataGrid DataBindingComplete 事件在哪里?
【发布时间】:2010-10-01 02:15:38
【问题描述】:

数据绑定完成后,我需要采取一些措施(例如,根据其他一些单元格将某些单元格设为只读)。在 WinForm DataGridView 中,我曾经在 DataBindingComplete 事件中执行此操作。但是,我在 WPF DataGrid 中找不到这样的事件。我还能用什么?

【问题讨论】:

  • 数据绑定完成后要做什么?
  • 正如我所说的,我想根据同一行中另一列的值禁用某些单元格。

标签: wpf events datagrid


【解决方案1】:

这是我想出来的:DataContextChanged 事件是正确使用的事件。唯一的问题是数据网格还没有准备好在我的代码中使用这个事件。但是,如果我像这样使用 Dispatcher.BeginInvoke,它就可以正常工作:

Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() => DoSomethingWithGrid()));

谁能解释为什么这是必要的?

实际上,在处理 WPF DataGrid 时,我不得不在很多情况下使用 Dispatcher 才能使其正常工作。为什么?

【讨论】:

【解决方案2】:

数据上下文已更改。

【讨论】:

  • 这很奇怪。不知何故 DataContextChanged 事件对我不起作用。我想要做的是根据同一行中另一列的值禁用列中的某些单元格。如果我将它放在单元格单击事件中,则相同的代码可以工作,但它在 DataContextChanged 事件中不起作用。我感觉触发此事件时 DataGrid 还没有准备好,这就是为什么我的更新没有生效 - 只是一个猜测。有什么线索吗?
  • 我认为您可以使用 IValueConverter 和样式设置轻松完成此操作。我可以为此编写一个示例,但需要几天时间。
  • 我不确定样式是否能解决我在这方面的所有问题。在我的 WinForm 应用程序(我正在尝试转换为 WPF)中,使用了很多 DataGridView。在 DataBindingComplete 事件中,我们做的不仅仅是设置视觉效果。但是样式肯定会为我省去一些麻烦。
  • @Chris:我对您使用 IValueConverter 和 Style 的建议非常感兴趣。你能给我一些示例代码吗?谢谢!
  • 此事件可能在数据绑定完成之前触发。请参阅msdn.microsoft.com/en-us/library/…的重要备注
【解决方案3】:

可能您正在使用线程,而 datagrid 并非所有 UI 组件都是线程安全的。

【讨论】:

    【解决方案4】:

    我想根据行的属性值为行着色,我尝试了很多事件(DataGrid.InitializedDataContextChangedAddingNewItemRowLoaded 等)以及BeginInvoke 的事情,但是没有任何效果。然后我发现:

    Loaded

    这个事件起到了作用,因为它允许我遍历我的行并根据需要为它们着色。

    private void SubjectsList_Loaded(object sender, RoutedEventArgs e)
    {
        Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() => ColorMyRows()));
    }
    

    CollorMyRows 看起来与 tihs 非常相似:

    private void ColorMyRows()
    {
        DataGridRow row = null;
        for (int i = 0; i < SubjectsList.Items.Count; i++)
        {
            // get one row
            row = SubjectsList.ItemContainerGenerator.ContainerFromIndex(i) as DataGridRow;
            if (myConditionIsFulfilled)
            {
                row.Background = Brushes.PaleGoldenrod; // black'n gold baby
                row.ToolTip = "This item fulfills the condition.";
            }
            else
            {
                row.Background = Brushes.PaleGreen;
                row.ToolTip = "This item does not.";
            }
        }
    }
    

    注意:如果您将ObservableCollection 绑定到DataGrid,则循环中的索引(DataGrid 行的索引)将对应于集合中的索引:)

    【讨论】:

      【解决方案5】:

      您可以将单元格只读属性绑定到模型的其他属性更改时更改的属性。 我之前的想法和你一模一样,但是我开始在模型中思考更多,而不是我对 DataGrid 不感兴趣的视图,而是绑定到的列表,你可以做同样的事情 我之前在类似的情况下使用过它

      public class Model : INotifyPropertyChanged
          {
      public bool IsChecked
              {
                  get { return isChecked; }
                  set
                  {
                      isChecked = value;
                      RaisePropertyChanged("IsChecked");
                      RaisePropertyChanged("Visibilty");
                  }
              }
      public Visibility Visibilty
              {
                  get
                  {
                      return IsChecked ? Visibility.Visible : Visibility.Hidden;
                  }
              }
      }
      

      它是绑定到 IsChecked 属性的数据网格中的一个复选框,其他单元格绑定到可见性,它对我有用。 希望对您有所帮助。

      【讨论】:

        【解决方案6】:

        您可以声明一个 BackgroundWorker 并尝试在 DoWork 事件中填充您的 GridView 并在 RunWorkerCompleted 事件中编写您的代码

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-05-10
          • 1970-01-01
          • 2011-04-22
          • 1970-01-01
          • 2011-07-21
          • 1970-01-01
          • 2016-03-31
          • 2015-05-31
          相关资源
          最近更新 更多