首页 > 解决方案 > 如果此列的标题包含点,为什么我无法设置 datagrid 单元格值?

问题描述

我的 wpf app xaml 很简单,只有一个数据网格:

<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);

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

为什么?

在此处输入图像描述

标签: wpfdatagrid

解决方案


我找到了解决方案,使用以下方法将 dot char 替换为另一个 char:

StringNameWithDot.Replace(".", "\x2024");

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

带点的 DataTable 列名称是什么导致它们不适合 WPF 的 DataGrid 控件?


推荐阅读