简体   繁体   English

WPF将datagrid绑定到可观察的字符串集合

[英]WPF binding datagrid to a string observable collection

I've always binded datagrid to observable collection of classes. 我一直将datagrid绑定到可观察的类集合。 Now it should be simpler: 现在应该更简单:

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

and then 接着

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

after

dtgCodes.ItemsSource = obcCodes;

so I expected to see those codes in the datagrid instead what I see is: 所以我希望在datagrid中看到这些代码,而不是:

在此处输入图片说明

thanks for any help 谢谢你的帮助

As explained here strings are immutable 如此处所述字符串是不可变的

so you will have to use 所以你将不得不使用

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

and then use it like that: 然后像这样使用它:

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

by doing that you will be able to modify or delete the strings in the datagrid. 这样,您将能够修改或删除数据网格中的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM