KentZhou
KentZhou

Reputation: 25583

How to get control parent DataContext for element to element databinding?

Suppose I have a user control which datacontext is bound to a VM. This VM has a property for a list MyList.

Inside this user control I have a ComboBox, I want to set following kind of xaml

   <Grid x:Name="LayoutRoot" Background="White">
            <StackPanel HorizontalAlignment="Stretch">
                <sdk:DataGrid ItemsSource="{Binding YourList}"  IsReadOnly="True" AutoGenerateColumns="False" >
                    <sdk:DataGrid.Columns>                      
                        <sdk:DataGridTextColumn  Header="Name"   Binding="{Binding Name}" />
                        <!-- ...... -->
                        <sdk:DataGridTemplateColumn  Header="User" >
                            <sdk:DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <ComboBox ItemsSource="{Binding ElementName=LayoutRoot, Path=DataContext.MyList}" DisplayMemberPath="Value" SelectedValuePath="Key" SelectedValue="{Binding UserID}" ></ComboBox>
                                </DataTemplate>
                            </sdk:DataGridTemplateColumn.CellTemplate>
                        </sdk:DataGridTemplateColumn>

                    </sdk:DataGrid.Columns>
                </sdk:DataGrid>
            </StackPanel>
    </Grid>

but it is not working.

How to resolve this problem?

Upvotes: 1

Views: 1805

Answers (2)

JBrooks
JBrooks

Reputation: 10013

This worked form me. This was the ItemSource for a ComboBox that was within a DataGrid:

      ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=sdk:DataGrid}, 
Path=DataContext.Teams}"> 

Upvotes: 1

Tom
Tom

Reputation: 836

Are you trying to get to the main VM from within the UserControl? take a look at this solution. http://weblogs.asp.net/dwahlin/archive/2009/08/20/creating-a-silverlight-datacontext-proxy-to-simplify-data-binding-in-nested-controls.aspx

Upvotes: 0

Related Questions