C# WPF代码隐藏:DataGridTextColumn显示列标题,但不显示来自使用xpath的xmlprovider的数据
我试图用XPath显示来自XMLProvider的数据。由于xml文件的动态特性,我需要通过代码隐藏加载数据 我正在寻找一个如下所示的数据网格:C# WPF代码隐藏:DataGridTextColumn显示列标题,但不显示来自使用xpath的xmlprovider的数据,c#,wpf,xpath,code-behind,C#,Wpf,Xpath,Code Behind,我试图用XPath显示来自XMLProvider的数据。由于xml文件的动态特性,我需要通过代码隐藏加载数据 我正在寻找一个如下所示的数据网格: Sensor BIS MAN BIS MAN ----------------------------- Sensor1 45 43 44 46 Sensor2 45 43 43 45 仅供参考:在网格上方,我将显示一个假超级标题,用于分组BIS和MAN值(以防您不理解为什么重复BIS和MAN关键字) da
Sensor BIS MAN BIS MAN
-----------------------------
Sensor1 45 43 44 46
Sensor2 45 43 43 45
仅供参考:在网格上方,我将显示一个假超级标题,用于分组BIS和MAN值(以防您不理解为什么重复BIS和MAN关键字)
datagrid的XAML代码:
<DataGrid Name="MDataGrid" AutoGenerateColumns="False"></DataGrid>
将显示传感器列的标题,但网格中没有列出任何数据。我哪里错了?很显然,你需要一个额外的装订,正如其他人指出的那样。我不知道为什么,但它做得很好
// loading xml for grid
XmlDocument doc = new XmlDocument();
doc.Load(_file_location_); // (it does find the xml)
// setting the xml provider
XmlDataProvider _xmlDataProvider = new XmlDataProvider();
_xmlDataProvider.Document = doc;
_xmlDataProvider.XPath = @"/ShipData/Draught/Sensor";
// Setting the datacontext (updated code)
Binding binding = new Binding();
binding.Source = _xmlDataProvider;
MDataGrid.SetBinding(DataGrid.ItemsSourceProperty, binding);
// Creating the column Sensor (as an example) and create a new binding for xpath
var dataGridTextColumn = new DataGridTextColumn();
Binding bindingSensor = new Binding();
bindingSensor.XPath = "@Name";
dataGridTextColumn.Binding = bindingSensor;
dataGridTextColumn.Header = "Sensor";
// other repeating columns (bis+man ...) omitted for this example
// adding the column to the datagrid
MDataGrid.Columns.Add(dataGridTextColumn);
它也适用于我想要添加的列(使用动态调整的XPath代码)
归功于:Magnus(MM8),正如您在回答中所述,这并不是额外的。这确实是必须采取的步骤。DataGrid显示提供给ItemsSource
属性的数据,因此如果未设置ItemsSource
,则不会显示任何数据。您可以在XAML中进行绑定,而不是在代码中手动设置ItemsSource
绑定:
<DataGrid Name="MDataGrid" ItemsSource="{Binding}" AutoGenerateColumns="False">
谢谢你的回答。我错误地认为xmlProvider会提供所有需要的详细信息,比如ItemsSource,但似乎没有。
// loading xml for grid
XmlDocument doc = new XmlDocument();
doc.Load(_file_location_); // (it does find the xml)
// setting the xml provider
XmlDataProvider _xmlDataProvider = new XmlDataProvider();
_xmlDataProvider.Document = doc;
_xmlDataProvider.XPath = @"/ShipData/Draught/Sensor";
// Setting the datacontext (updated code)
Binding binding = new Binding();
binding.Source = _xmlDataProvider;
MDataGrid.SetBinding(DataGrid.ItemsSourceProperty, binding);
// Creating the column Sensor (as an example) and create a new binding for xpath
var dataGridTextColumn = new DataGridTextColumn();
Binding bindingSensor = new Binding();
bindingSensor.XPath = "@Name";
dataGridTextColumn.Binding = bindingSensor;
dataGridTextColumn.Header = "Sensor";
// other repeating columns (bis+man ...) omitted for this example
// adding the column to the datagrid
MDataGrid.Columns.Add(dataGridTextColumn);
<DataGrid Name="MDataGrid" ItemsSource="{Binding}" AutoGenerateColumns="False">
......
// Setting the datacontext for
MDataGrid.DataContext = _xmlDataProvider;
......