WPF代码落后:DataGridTextColumn显示列标题,但不显示xmlprovider与xpath的数据

时间:2014-03-25 10:20:50

标签: 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关键词重复)

datagrid的XAML代码:

<DataGrid Name="MDataGrid" AutoGenerateColumns="False"></DataGrid>

这是XML文件样本数据:

<?xml version="1.0" encoding="utf-8" ?>
<ShipData>
  <Draught>
    <Measures>
      <Mdata>M1</Mdata>
      <Mdata>M2</Mdata>
    </Measures>
    <Sensor Name="Sensor1">
      <Measurement>
        <Bis>45</Bis>
        <Man>43</Man>
      </Measurement>
      <Measurement>
        <Bis>44</Bis>
        <Man>46</Man>
      </Measurement>
    </Sensor>
    <Sensor Name="Sensor2">
      <Measurement>
        <Bis>45</Bis>
        <Man>43</Man>
      </Measurement>
      <Measurement>
        <Bis>43</Bis>
        <Man>45</Man>
      </Measurement>
    </Sensor>
  </Draught>
</ShipData>

以下是我使用的c#代码背后的代码:

// 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 for 
MDataGrid.DataContext = _xmlDataProvider; 

// Creating the column Sensor (as an example) and create a new binding for xpath
var dataGridTextColumn = new DataGridTextColumn();

Binding bindingSensor = new Binding();
bindingSensor.Source = _xmlDataProvider;
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);

显示传感器列的标题,但网格中未列出任何数据。我哪里错了?

2 个答案:

答案 0 :(得分:1)

当你在回答中说明时,那不是额外的。这确实是必需的步骤。 DataGrid显示提供给ItemsSource属性的数据,因此如果未设置ItemsSource,则不会显示任何数据。您可以在XAML中执行此操作,而不是在代码中手动设置ItemsSource绑定:

<DataGrid Name="MDataGrid" ItemsSource="{Binding}" AutoGenerateColumns="False">

并将代码隐藏设置为DataContext

......
// Setting the datacontext for 
MDataGrid.DataContext = _xmlDataProvider; 
......

答案 1 :(得分:0)

显然你需要一个额外的绑定,因为别人指出了我。我不知道究竟为什么,但它完美地完成了这项工作。

// 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代码)

归功于:马格努斯(MM8)