【问题标题】:Changing data when itemsource is specified for a datagrid in wpf在 wpf 中为数据网格指定 itemsource 时更改数据
【发布时间】:2014-06-03 23:15:01
【问题描述】:

我有一个绑定到自定义类型的 ObservableCollection 的数据网格。该类型有一个布尔属性“IsCopying”。 “绑定”是通过将网格的 ItemSource 属性指定到 ObservableCollection 来完成的。

我想在数据绑定到网格之前更改数据。网格只需要是数据的只读视图。我已经使用 AutoGeneratingColumn 事件更改了列标题:

if (e.Column.header.ToString() == "IsCopying")
{
   DataGridTextColumn t = new DataGridTextColumn();
   t.header = "Status";
   e.Column = t;
}

...效果很好。我想在将单个属性绑定到每行的单元格期间做同样的事情。我觉得它会像这样工作:

//NOT REAL CODE!!!!!!!
private void dgItem_CellBinding(object sender, DataGridCellBindingEventArgs e)
{
   MyCustomType theItem = e.(MyCustomType)ObjectGettingBound;
   if (theItem.IsCopying == true)
   {
      e.TypeGettingBound= DataGridCellType.Text //or however this works;
      e.DataToBind = "Working...";
    }else{
      e.TypeGettingBound = DataGridCellType.Text;
      e.DataToBind = "Waiting for command...";
   }
}

希望以上内容有意义。我找不到单个单元格何时绑定以及如何拦截它的事件。我确信这很常见,但我对此很陌生,在 SO 上找不到任何解决这个特定问题的东西。也许我的方法不对?

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    您的自定义类型是否实现了 INotifyPropertyChange?

    您能否向您的自定义类型添加一个引发 PropertyChange 事件并公开您需要的文本帽子的公共属性?

    public class MyCustomType: INotifyPropertyChanged
    {
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected void OnPropertyChanged(string name)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(name));
                }
            }
    
            private bool _IsCopying;
    
            public bool IsCopying
            {
                get { return _IsCopying; }
                set
                {
                    _IsCopying = value;
                    OnPropertyChanged("IsCopyingText");
                }
            }
    
            public string IsCopyingText
            {
                get
                {
                    if(IsCopying) return "Working...";
                    else return "Waiting for command...";
                }
            }
    }
    

    然后将列绑定到 IsCopyingText 属性,如果不想显示 IsCopying 列,请在 AutoGeneratingColumn 事件中将其 Cancel 属性设置为 True (http://msdn.microsoft.com/en-us/library/cc903950(v=vs.95).aspx)

    使网格只读:http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.isreadonly.aspx

    另一种方法是使用 IValueConverter,如下所示:

    WPF Datagrid - Column Binding related to other columns

    【讨论】:

    • 不,我还没有实现 INotifyPropertyChanged.... 除非我遗漏了与问题无关的内容。我不关心网格的更新。我要做的是,在“绑定时间”更改发送到单元格的值。
    • Arie,对不起,我想我明白你的意思了。我宁愿不必修改 MyCustomType 类……这就是我正在查看类中的数据并且只想在绑定时对其进行调整而不必重新编写类的问题。如果我可以重做这个类,我只需将布尔类型更改为字符串或添加另一个属性。
    • @rune711 那么我认为 IValueConverter 是适合您的方法。这样你就不需要接触你的自定义类了。
    猜你喜欢
    • 2012-10-26
    • 2013-06-18
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    相关资源
    最近更新 更多