I have a DataGrid like below
<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
. The actual path I need is 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.
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
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.