I have 2 ObservableCollection lists, which we can call A and B, then I have a GridView that I want to bind to list A and a ComboBox INSIDE that GridView, that I want to bind to list B. I've set the ItemsSource property of the GridView by code: gridview.ItemsSource=A (and it works!). About the ComboBox its instance it is not available by code, I suppose because its definition it is enclosed between the DataTemplate tags; so I wonder how to bind the combo to list B, either by code or by XAML.

Follows the XAML code:

<ListView Grid.Row="0" Grid.Column="1"  HorizontalAlignment="Stretch" Margin="0,0,0,0" Name="lstReplacements" VerticalAlignment="Stretch">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn HeaderContainerStyle="{StaticResource MyHeaderStyle}" Header="Wrong text" DisplayMemberBinding="{Binding Word}"/>
                            <GridViewColumn HeaderContainerStyle="{StaticResource MyHeaderStyle}" Header="Replacement" Width="60" DisplayMemberBinding="{Binding Replacement}" />
                            <GridViewColumn HeaderContainerStyle="{StaticResource MyHeaderStyle}" Header="Type"  Width="30">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <ComboBox ItemsSource="{??????}" DisplayMemberPath="??????"  Grid.Row="1" Grid.Column="0" Name="cmbCorrectionType"  Width="75" />
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                        </GridView>
                    </ListView.View>
                </ListView>

Thanks in advance for the support! Chris

有帮助吗?

解决方案

I assume this control is in UserControl and you have set DataContext of that UserControl to the class instance where your both collections CollectionA and CollectionB resides.

You can then bind using RelativeSource:

<ComboBox ItemsSource="{Binding DataContext.CollectionB,
                               RelativeSource={RelativeSource Mode=FindAncestor, 
                                              AncestorType=UserControl}}"/>

Also you can set DataContext of ListView to the class instance and all you need to do is change AncestorType to ListView in place of UserControl in above binding.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top