Binding Contexts

Topics: Conventions, Getting Started
Sep 19, 2013 at 3:25 AM
I have a question about setting the data context for various items. I have a DataGrid that is bound by CM convention to a property in my VM.
            <DataGrid x:Name="Mappings"
                  AutoGenerateColumns="False" 
                  HeadersVisibility="Column" 
                  CanUserReorderColumns="False" 
                  CanUserResizeColumns="False" 
                  CanUserSortColumns="False" 
                  SelectedItem="{Binding SelectedMapping, Mode=TwoWay}"
                  ColumnWidth="SizeToCells">
                <DataGrid.Columns>
                         ... column definitions ...
                </DataGrid.Columns>
            </DataGrid>
I want to bind the selected item to a set of controls for editing (I do not want in-place edits). So I tried the following:
        <GroupBox Header="Edit Mapping Details" DockPanel.Dock="Right" DataContext="{Binding ElementName=Mappings, Path=SelectedItem}">
            <GroupBox.Template>
                <ControlTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="120" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Label Content="Source IOS" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="3,5" />
                        <TextBox Text="{Binding "{Binding Field1}" Grid.Column="1" />
                    </Grid>
                </ControlTemplate>
            </GroupBox.Template>
         </GroupBox>
This works and the property Field1 inside the selected item is correctly shown, can be edited and reflects the change in the datagrid.

Now what I really want to do is to replace the TextBox by a ComboBox whose values are populated via a VM property and the SelectedItem is set to the value of Field1. I tried the following for the ComboBox
<ComboBox ItemsSource="{Binding SourceList}" SelectedItem="{Binding Field1}" Grid.Column="1" />
and I have a property in my VM called SourceList which returns a BindableCollection<string>. This doesn't seem to work though - the SourceList never gets bound because I suspect it is using the Selected Item datacontext.

As a WPF beginner, this is a little over my head - can anyone out there show me how I can set up the datacontexts so that the CM convention will bind SourceList for me.

Regards
Alan