WPF datagrid XML绑定使用DataTemplate在单元格中显示多个项目

时间:2021-08-22 21:48:20

I have a DataGrid which is as follows::

我有一个DataGrid,如下所示::

<wpfkit:DataGrid AutoGenerateColumns="False"
       ItemsSource="{Binding}"
       Width="Auto"
       FrozenColumnCount="2"
       SelectionMode="Extended"
       CanUserAddRows="False"
       x:Name="CommonPEGrid"
       Loaded="CommonPEGrid_Loaded">
    <wpfkit:DataGrid.DataContext>
        <XmlDataProvider Source="PE.xml" XPath="/Rows/Row"></XmlDataProvider>
    </wpfkit:DataGrid.DataContext>
</wpfkit:DataGrid>

I am binding it from XML to DataGrid. My XML is as follows::

我将它从XML绑定到DataGrid。我的XML如下::

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

<Row Id="2">
  <Devices>
    <Device>Device 3</Device>
    <Device>Device 4</Device>
  </Devices>
</Row>

I have a DataTemplate for a cell in DataGrid defined as follows ::

我在DataGrid中有一个DataTemplate,定义如下::

<DataTemplate x:Key="MethodDefault">
    <ComboBox Margin="5" Height="25" ItemsSource="{Binding XPath=./Devices}"  SelectedIndex="0" 
                       >
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding XPath=./Device}"></TextBlock>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
</DataTemplate>

The problem is it always displays only 1 Device i.e first device in combobox. I want to display all Devices in a dropdown. I dont know how to iterate through them. I had thought that ComboBox will automatically iterate which is not the case. Please help me!!

问题是它总是只显示1个设备,即组合框中的第一个设备。我想在下拉列表中显示所有设备。我不知道如何迭代它们。我原以为ComboBox会自动迭代而不是这种情况。请帮帮我!!

1 个解决方案

#1


0  

I could figure out the answer. I am posting it assuming it helps someone !!

我可以找到答案。我发布它假设它可以帮助某人!

<ComboBox  ItemsSource="{Binding XPath=.//Devices}"  SelectedIndex="0" >

</ComboBox>

#1


0  

I could figure out the answer. I am posting it assuming it helps someone !!

我可以找到答案。我发布它假设它可以帮助某人!

<ComboBox  ItemsSource="{Binding XPath=.//Devices}"  SelectedIndex="0" >

</ComboBox>