Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t将WPF DataGrid绑定到具有名称、空格和其他特殊字符的列的表_Wpf_Wpf Controls_Wpfdatagrid - Fatal编程技术网

Can';t将WPF DataGrid绑定到具有名称、空格和其他特殊字符的列的表

Can';t将WPF DataGrid绑定到具有名称、空格和其他特殊字符的列的表,wpf,wpf-controls,wpfdatagrid,Wpf,Wpf Controls,Wpfdatagrid,当我有一个简单的数据集,它有一个具有简单列名(即没有空格或句点)的表时,下面这样的代码工作得很好: DataGrid resultsGrid=...; // Actually defined in the XAML DataSet ds=...; // The is the DataSet that contains one table Binding binding = new Binding(); resultsGrid.DataContext=ds.Tables[0];

当我有一个简单的
数据集
,它有一个具有简单列名(即没有空格或句点)的表时,下面这样的代码工作得很好:

  DataGrid resultsGrid=...; // Actually defined in the XAML
  DataSet ds=...; // The is the DataSet that contains one table
  Binding binding = new Binding();

  resultsGrid.DataContext=ds.Tables[0];
  resultsGrid.SetBinding(DataGrid.ItemsSourceProperty, binding);
上例中的DataGrid将
AutoGenerateColumns
属性设置为
True
,并从
数据集中的表中正确填充其数据

但是,如果我的表中有名称包含空格/句点或其他特殊字符的列,则自动绑定似乎会失败。我会遇到如下错误:

System.Windows.Data信息:20:BindingExpression无法 由于缺少信息而检索值。BindingExpression:Path=My 上校姓名;DataItem='DataRowView'(HashCode=8146871);目标元素 是“文本块”(名称=“”);目标属性为“Text”(类型为“String”)


显然,自动生成的绑定表达式
Path=My Col.Name
无效。整个路径需要“引用”,以便允许空格和句点。对于具有更复杂列名的表,有没有办法坚持使用
自动生成列
,或者我现在必须全部手动操作?

您可以将SQL select格式化为没有空格

 select [Column A] as 'Column_A' from table

您可以尝试处理
resultsGrid.AutoGeneratingColumns
,或者更可取的做法是,在集合数据绑定之前对网格执行此操作,以替换或删除列名中的空格或句点。但是,您必须同时对网格和数据集执行此操作,因此,如果您的数据被编辑和保存,此方法可能会非常麻烦。这项工作的效率和可行性在很大程度上取决于你的应用程序的结构和行为,加上我还没有对它进行过测试,所以对此要慎重考虑


据我所知(无可否认,这不是一个非常广泛的领域),你几乎被这种行为所困扰。我建议向微软提交一个bug;这似乎是
AutoGenerateColumns

的理想行为这正是我现在正在做的,在将数据集中的列绑定到网格之前对其进行重命名-非常困难。您可以修改存储过程吗?