[英]WPF DataGrid DataGridComboBoxColumn ItemsSource Referenced Off DataGrid.ItemsSource
I have a DataGrid like below 我有一个像下面的DataGrid
<DataGrid ItemsSource="{Binding Collection}" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Col1" Binding="{Binding Col1}" IsReadOnly="True" />
<DataGridComboBoxColumn Header="Col2" ItemsSource="{Binding Col2}" SelectedItemBinding="{Binding Selected}" />
</DataGrid.Columns>
</DataGrid>
The values that end up in Col1 are actually Window.DataContext.Collection[index].Col1
, but WPF seems to be looking for Col2's ItemsSource at Window.DataContext.Col2
. 在Col1中最终得到的值实际上是
Window.DataContext.Collection[index].Col1
,但是WPF似乎在Window.DataContext.Col2
寻找Col2的ItemsSource。 The actual path I need is Window.DataConext.Collection[index].Col2
我需要的实际路径是
Window.DataConext.Collection[index].Col2
Please note the reference to index
above is not to a static value but to the fact that each row gets a value from one of the collection items. 请注意,上面对
index
的引用不是静态值,而是每一行从其中一个收集项中获取值的事实。
How do I accomplish this? 我该如何完成?
Try this: 尝试这个:
<DataGridComboBoxColumn Header="Col2" SelectedItemBinding="{Binding Selected}"
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Window}},
Path=DataContext.Collection[index].Col2}" />
Poor formatting. 格式不佳。 (insert a number for
index
) (为
index
插入一个数字)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.