WPF 数据网格 XML 绑定使用 DataTemplate 在单元格中显示多个项目
我有一个 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>
我将它从 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>
我在 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>
问题是它始终仅显示 1 个设备,即组合框中的第一个设备。我想在下拉列表中显示所有设备。我不知道如何迭代它们。我原以为 ComboBox 会自动迭代,但事实并非如此。请帮助我!!
I have a DataGrid which is as follows::
<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::
<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 ::
<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!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能找出答案。我发布它假设它对某人有帮助!
I could figure out the answer. I am posting it assuming it helps someone !!