使用DataGridTemplateColumn将数据网格数据绑定到XML

时间:2021-08-05 21:50:14

I have an XML like this ::

我有这样的XML ::

<?xml version="1.0" encoding="utf-8" ?>
<Rows>
  <Row Id="1">
     <Devices>
       <Device DeviceId="123">Device 1</Device>
       <Device DeviceId="abcd" >Device 2</Device>
     </Devices>
    <Methods>
       <Method>Method 1</Method>
       <Method>Method 2</Method>
     </Methods>      
</Row>

<Row Id="2">
  <Devices>
    <Device>Device 1</Device>
    <Device>Device 2</Device>
  </Devices>
  <Methods>
    <Method>Method 1</Method>
    <Method>Method 2</Method>
  </Methods>  
  </Row>   
</Rows>

I have to DataBind the above XML to a DataGrid.

我必须将DataBind上面的XML绑定到DataGrid。

My DataGrid is as follows:

我的DataGrid如下:

       <wpfkit:DataGrid AutoGenerateColumns="False" DataContext="{Binding Grid}"
               ItemsSource="{Binding Path=Elements[Row]}"
               Width="Auto"
               FrozenColumnCount="2"
               SelectionMode="Extended"
               CanUserAddRows="False"
               x:Name="CommonPEGrid"
               Loaded="CommonPEGrid_Loaded">
           </wpfkit:DataGrid>

My code where I am associating DataContext is as follows:: I have created some template columns in code behind and associated DataTemplate with them.

我关联DataContext的代码如下::我在代码后面创建了一些模板列,并将DataTemplate与它们相关联。

public class MainViewModel : ViewModelBase
{
    public MainViewModel()
    {
        Grid = XElement.Load("PE.xml");  
    }

    public XElement Grid
    {
        get;
        set;
    }       
}

My codebehind is as follows :::

我的代码隐藏如下:::

  public partial class MainView : Window
 {
    DataGrid dg;

    public MainView()
    {
        InitializeComponent();
    }

    private void CommonPEGrid_Loaded(object sender, RoutedEventArgs e)
    {
        dg = sender as DataGrid;

        DataGridTemplateColumn column = null;

        column = new DataGridTemplateColumn();
        column.Header = "Device";
        column.CellTemplate = this.FindResource("DeviceDefault") as DataTemplate;
        dg.Columns.Add(column);

        column = new DataGridTemplateColumn();
        column.Header = "Commands";
        column.CellTemplate = this.FindResource("MethodDefault") as DataTemplate;
        dg.Columns.Add(column);
    }

}

Now I don't know how to display the elements in XML in a ComboBox using DataTemplate. It gives me so many errors::: Also the combobox items is always empty :( :( . Where am I going wrong. Experts please guide me !!! My code is as follows::

现在我不知道如何使用DataTemplate在ComboBox中显示XML中的元素。它给了我很多错误:::组合框项目总是空的:( :(。我哪里错了。专家请指导我!!!我的代码如下::

 <DataTemplate x:Key="DeviceDefault">
       <ComboBox ItemsSource="{Binding XPath=Devices}"  SelectedIndex="0"  TextSearch.TextPath="Value" >
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Width="auto" FontSize="9" FontWeight="2"  Height="auto" Margin="2" Text="{Binding Element[Device].Value}"></TextBlock>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </DataTemplate>

1 个解决方案

#1


0  

Solved it ::

解决了::

The link from * . I had asked a similar question to this and both have the same answer.

*的链接。我曾向此提出过类似的问题,两者都有相同的答案。

#1


0  

Solved it ::

解决了::

The link from * . I had asked a similar question to this and both have the same answer.

*的链接。我曾向此提出过类似的问题,两者都有相同的答案。