【问题标题】:Why I cannot set datagrid cell value if this column's header contains a dot?如果此列的标题包含点,为什么我无法设置 datagrid 单元格值?
【发布时间】:2018-12-08 04:22:06
【问题描述】:

我的wpf app xaml很简单,只有一个datagrid:

<DataGrid ItemsSource="{Binding UserCollection}"/>

在后面的代码中,UserCollection 是一个 DataTable:

private DataTable userCollection;
public DataTable UserCollection
{
    get { return this.userCollection; }
    set
    {
        this.userCollection = value;
        NotifyPropertyChanged();
    }
}

在 ViewModel.cs 中,代码很简单,创建 2 列并添加一行:

this.UserCollection = new DataTable();
UserCollection.Columns.Add("col1", typeof(int));
UserCollection.Columns.Add("co.l2", typeof(int));

var r=UserCollection.NewRow();
r["col1"] = 1;
r["co.l2"] = 2;
UserCollection.Rows.Add(r);

但是听者中包含点的第二列无法在UI中显示该值,您可以看到图片。

为什么?

【问题讨论】:

    标签: wpf datagrid


    【解决方案1】:

    我找到了解决办法,用 :

    将 dot char 替换为另一个 char
    StringNameWithDot.Replace(".", "\x2024");
    

    Wpf 数据表列标题不能包含“.”、“/”、“[]”,这里是另一个链接:

    What is it about DataTable Column Names with dots that makes them unsuitable for WPF's DataGrid control?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      • 2012-03-18
      • 1970-01-01
      • 1970-01-01
      • 2012-01-25
      相关资源
      最近更新 更多