如何在标题中使用时,“/”,以防止在WPF DataGrid中不显示列? [英] How to prevent columns from not displaying in WPF datagrids when using '/' in the header?
问题描述
我有一个WPF的Datagrid在我的应用程序,我的的ItemSource的值设置为我构建了一个DataTable的默认视图。问题在于,每当我设置的栏目之一的ColumnName在我的DataTable到包含字符串'/'的标题显示在DataGrid控件但数值没有。我怎样才能解决这个得到什么?
这是用'/'替换为同一张桌子 -
顺便说一句,这也似乎与头发生在他们有一个。。因此,任何小数会导致相同的行为。
我的电网被定义为
< DataGrid的X:名称=dgLFKPI/>
和我在后面的代码设定值(是的,它应该是一个视图模型和MVVM,但它是一个遗留的应用程序,正在缓慢移动这种方式)。
dgLFKPI.ItemsSource = dt.DefaultView;
在列名称中的特殊字符不正确地解析结合路径分析器。
所以,一列绑定到 3/4
实际上只绑定属性 3
,而不是财产 3/4
。 (有同样的事情在。
绑定)
您可能会看到在此同时,调试窗口绑定错误。正在运行应该说同样的事情
System.Windows.Data错误:40:BindingExpression路径错误:
在'对象'''DataRowView的
块引用> $没有发现'3'属性b
$ b据的这个答案
有一些具有特殊意义$ b不同的字符$ b在包括句号一个绑定路径(。),斜杠(/),方
括号('[',']')和括号('('')')中,括号将
会导致您的应用程序崩溃。这些特殊字符可以通过与周围的方括号中的绑定路径
进行转义。有关路径和字符转义的更多信息
可以在[装订
声明概述]中找到[2]
块引用>
这个链接的问题也包含了很好的解决方案对付那些想使用自动生成的列网格。
使用的
AutoGeneratingColumn
事件,并以他们的名义逃避他们手动创建这些特殊字符列绑定。用方括号< DataGrid的X:名称=dgLFKPI
AutoGeneratingColumn =dgLFKPI_AutoGeneratingColumn/> ;
私人无效dgLFKPI_AutoGeneratingColumn(对象发件人,DataGridAutoGeneratingColumnEventArgs E)
{
如果(e.PropertyName.Contains(/)及和放大器; e.Column是DataGridBoundColumn)
{
VAR山坳= E .Column为DataGridBoundColumn;
col.Binding =新的Binding(的String.Format([{0}],e.PropertyName));
}
}
I have a WPF Datagrid in my application where I set the value of the ItemSource to the DefaultView of a DataTable that I have constructed. The problem lies in the fact that whenever I set the ColumnName of one of the columns in my DataTable to a string that includes '/' the header shows up in the DataGrid control but the values do not. How can I get around this?
This is the same table with the '/' replaced with '-'
as an aside this also appears to happen with headers that have a '.' in them. So any decimal will cause the same behavior.
My grid is defined as
<DataGrid x:Name="dgLFKPI" />
and I am setting the value in code behind (yes it should be in a viewmodel and MVVM but it is a legacy app that is slowly moving that way).
dgLFKPI.ItemsSource = dt.DefaultView;
解决方案The special characters in the column names are incorrectly parsed by the binding path parser.
So a column binding to
3/4
is actually only binding to the property3
, and not the property3/4
. (The same thing with the.
binding)You'll probably see binding errors in the debug window while this is running that should say the same sort of thing.
System.Windows.Data Error: 40 : BindingExpression path error: '3' property not found on 'object' ''DataRowView'
According to this answer
There are a number of different characters which have special meaning in a binding path including full stop ('.'), slash ('/'), square brackets ('[',']') and parenthesis ('(',')'), the parenthesis will cause your app to crash. These special characters can be escaped by surrounding the binding path with square brackets. More information about paths and character escaping can be found in the [Binding Declarations Overview][2]
That linked question also contains a good solution for dealing with grids that want to use auto-generated columns.
Use the
AutoGeneratingColumn
event, and manually create the bindings for columns with these special characters in their name to escape them using the square brackets.<DataGrid x:Name="dgLFKPI" AutoGeneratingColumn="dgLFKPI_AutoGeneratingColumn" />
private void dgLFKPI_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { if (e.PropertyName.Contains('/') && e.Column is DataGridBoundColumn) { var col = e.Column as DataGridBoundColumn; col.Binding = new Binding(string.Format("[{0}]", e.PropertyName)); } }
这篇关于如何在标题中使用时,“/”,以防止在WPF DataGrid中不显示列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!