【问题标题】:WPF binding datagrid to a string observable collectionWPF 将数据网格绑定到字符串可观察集合
【发布时间】:2016-12-15 10:44:44
【问题描述】:

我一直将数据网格绑定到可观察的类集合。 现在应该更简单了:

public ObservableCollection<string> obcCodes{ get; set; }

然后

if (obcCodes== null)
    obcCodes= new ObservableCollection<string>();
obcCodes.Add("K2001");
obcCodes.Add("K2002");
obcCodes.Add("K2003");
obcCodes.Add("K2004");

之后

dtgCodes.ItemsSource = obcCodes;

所以我希望在数据网格中看到这些代码,而不是我看到的是:

感谢您的帮助

【问题讨论】:

  • 也许您应该为您的项目控件设置 DataTemplate 以查看您的数据。

标签: c# datagrid bind observablecollection itemssource


【解决方案1】:

正如here 解释的那样,字符串是不可变的

所以你必须使用

    public class StringWrapper { public string Text { get; set; } }

然后像这样使用它:

    obcCodes.Add(new StringWrapper() { Text = "K2001" });

通过这样做,您将能够修改或删除数据网格中的字符串。

【讨论】:

  • 一个非常微不足道的补充:如果你像那样改变类---> public class StringWrapper {public StringWrapper(string str) { Text = str; } 公共字符串文本 { 获取;放; } } 你将能够像这样添加 ----> obcCodes.Add(new StringWrapper("K2001"));
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-21
  • 1970-01-01
  • 1970-01-01
  • 2015-03-07
  • 2020-12-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多