【问题标题】:How to cope with a observable collection binded datagrid with manual update如何通过手动更新处理 observablecollection 绑定数据网格
【发布时间】:2017-04-27 06:04:52
【问题描述】:

我有一个数据网格

<DataGrid Name="dtgFeatures" Height="100" Margin="10" ColumnWidth="*" CanUserAddRows="True" MouseLeftButtonUp="DtgFeatures_MouseLeftButtonUp"/>

绑定到一个可观察的集合

ObservableCollection<CfgPartPrograms> obcCfgPartPrograms = new ObservableCollection<CfgPartPrograms>();

[Serializable]
public class CfgPartPrograms
{
    public CfgPartPrograms() { }
    public string Group{ get; set;}
    public string Description{ get; set;}
    public string Filename{ get; set;}<------set with openfiledialog
    public string Notes{ get; set;}
}

现在,因为我希望能够使用 openfileDialog 插入文件名,所以我添加了以下代码:

private void DtgFeatures_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    int column = (sender as DataGrid).CurrentCell.Column.DisplayIndex;
    if ( column == 2)
    {
        OpenFileDialog ofdPP = new OpenFileDialog();
        if (ofdPP.ShowDialog() == true)
        {
            if (obcCfgPartPrograms.Count == 0)
                obcCfgPartPrograms.Add(new CfgPartPrograms() { Filename = ofdPP.FileName });
            else
                obcCfgPartPrograms[selectedIndex].Filename = ofdPP.FileName;
            dtgFeatures.ItemsSource = null;
            dtgFeatures.ItemsSource = obcCfgPartPrograms;
        }
    }

问题是,当我设置文件名时,可观察集合尚未更新。 我会用图片来解释:

所以我现在添加了 aaaa 和 bbb,我想使用上面的代码强制文件名,但是当我这样做时,尚未对 observable 集合执行绑定操作,因此 aaaa 和 bbbb 不存在。

简而言之如何告诉绑定的数据网格更新绑定??

提前致谢 帕特里克

【问题讨论】:

    标签: c# wpf data-binding datagrid observablecollection


    【解决方案1】:

    我找到了here:我错过了

    dtgFeatures.CommitEdit(DataGridEditingUnit.Row, true);
    

    命令

    【讨论】:

      【解决方案2】:

      您的 CfgPartPrograms 类应实现 INotifyPropertyChanged 接口并在数据绑定属性设置为新值时引发 PropertyChanged 事件:https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx

      [Serializable]
      public class CfgPartPrograms : System.ComponentModel.INotifyPropertyChanged
      {
          public CfgPartPrograms() { }
      
          public string Group { get; set; }
          public string Description { get; set; }
          private string _fileName;
      
          public string Filename
          {
              get { return _fileName; }
              set { _fileName = value; NotifyPropertyChanged(); }
          }
      
          public string Notes { get; set; }
      
          public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
      
          private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
          {
              if (PropertyChanged != null)
                  PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
          }
      }
      

      【讨论】:

      • 好的,我认为您的想法是正确的,这就是解决方案,但它不起作用。首先,我认为必须在文件名更改时而不是在其他成员更改时更新可观察集合。因此,我已将更改的通知属性放在组上。但除此之外,在数据网格中输入数据并按回车键后,组属性的设置代码没有被击中。
      • 简而言之,对数据网格的手动更改不会反映在可观察的集合上
      • 默认情况下,当您离开该行时将设置源属性。您不需要显式调用 CommitEdit 方法,除非您想通过单元格提交更改:blog.scottlogic.com/2009/01/21/…。这是另一个问题,而不是你原来的问题。如果您的问题已经解决,请记得接受答案。
      • 我找到了解决办法thanx
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 2016-11-01
      • 2013-01-30
      • 1970-01-01
      相关资源
      最近更新 更多