【问题标题】:Datagrid databinding to XML with DataGridTemplateColumn使用 DataGridTemplateColumn 将 Datagrid 数据绑定到 XML
【发布时间】:2011-05-04 21:46:58
【问题描述】:

我有一个这样的 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>

我必须将上述 XML 数据绑定到 DataGrid。

我的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>

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

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

    public XElement Grid
    {
        get;
        set;
    }       
}

我的代码如下:::

  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);
    }

}

现在我不知道如何使用 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>

【问题讨论】:

    标签: wpf datagrid combobox datatemplate xml-binding


    【解决方案1】:

    解决了::

    链接from StackOverflow。我曾经问过一个与此类似的问题,并且两者都有相同的答案。

    【讨论】:

      猜你喜欢
      • 2020-07-21
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 2011-04-06
      • 2011-05-14
      • 2014-10-23
      • 2011-07-09
      • 1970-01-01
      相关资源
      最近更新 更多