代码之家  ›  专栏  ›  技术社区  ›  Gurucharan Balakuntla Maheshku

使用DataGridTemplateColumn将Datagrid数据绑定到XML

  •  0
  • Gurucharan Balakuntla Maheshku  · 技术社区  · 14 年前


    <?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数据绑定到数据网格。

    我的数据网格如下:

           <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 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 回复  |  直到 14 年前
        1
  •  0
  •   Community datashaman    7 年前

    解决了:

    from StackOverflow . 我问了一个类似的问题,两个都有相同的答案。