wpf datatable列名称不能有斜杠/ [英] wpf datatable column name can not have slash /
问题描述
我是WPF的新手.我有一个很奇怪的问题,就是要做一张桌子,这是我的代码:
I'm very new to WPF. I'm having a very weird issue to just make a table, here is the code I have:
DataTable table = new DataTable();
table.Columns.Add("request/sec", typeof(string));
DataRow newRow = dataTable.NewRow();
newRow["request/sec", "na");
但是它只显示带有"request/sec"的列名和一个空行
but it only shows a column name with "request/sec" and an empty row
但是,如果我这样做:
DataTable table = new DataTable();
table.Columns.Add("test(request/sec)", typeof(string));
DataRow newRow = dataTable.NewRow();
newRow["test(request/sec)", "na");
然后它可以正常工作,它正确显示了列名,并显示了一个值为"na"的行
then it's working, it shows the column name correctly, and a row with value "na"
我不知道为什么会这样,我知道这听起来很荒谬.有人可以帮忙吗?
I have no idea why this is happening, I know it sounds ridiculously. Can anyone help?
注意:我也尝试过"//","/","\/",@我无法对列名进行硬编码,它是由某些逻辑动态生成的,我以上面的代码为例
note: I also tried "//", "/", "\/", @ I can not hardcoded the column names, it's dynamic generated by certain logic, I used the above code as examples
推荐答案
当属性名称包含某些保留字符(如.
和/
)时,创建绑定会出现问题.属性路径解析器只是不处理它们.您有两种选择:
There are issues creating bindings when a property name contains some reserved characters like .
and /
. The property path parser just doesn't handle them. You have a couple options:
-
不是让网格自动生成列,而是自己定义它们,对于具有保留字符的列,请绑定到数据行的索引器而不是列名称:
Instead of having the grid automatically generate the columns, define them yourself, and for columns with reserved characters, bind to the data row's indexer instead of the column name:
<DataGridTextColumn Binding="{Binding Path=[request/sec]}" Header="request/sec" />
如果仅因为要控制列标题文本而只关心列名称,只需手动声明列,为列指定合法"名称,然后设置 Header
就像上面一样.
这篇关于wpf datatable列名称不能有斜杠/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!