Override parent datacontext

I'm learning about databinding at the moment, and facing some trouble with parent and child datacontexts.

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
        DataContext="{Binding Group}"
        d:DataContext="{Binding Groups[0], Source={d:DesignData Source=/DataModel/SampleData.json, Type=data:SampleDataSource}}">
        <Grid.ChildrenTransitions>
            <TransitionCollection>
                <EntranceThemeTransition/>
            </TransitionCollection>
        </Grid.ChildrenTransitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>


 <StackPanel x:Name="SideData" Width="480" Margin="12,12,24,12" Grid.Row="1" HorizontalAlignment="Left" DataContext="{Binding SideItem}">
        <TextBlock x:Name="SideText"        Text="{Binding Title}" Margin="12,12,12,12" Style="{StaticResource SubheaderTextBlockStyle}" MaxHeight="60"/>
        <Image x:Name="SideImage"           Source="{Binding ImagePath}" Height="400" Width="400"  Margin="0,0,0,20" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}"/>
        <TextBlock x:Name="SideDescription" Text="{Binding Subtitle}" Margin="0,0,0,0" Style="{StaticResource BodyTextBlockStyle}"/>
    </StackPanel>

I want to leave the DataContext for the Grid at "Group", but I want all the controls inside the StackPanel to be bound to "SideItem".

I'm sure I've done it wrong as it isn't working properly. When I bind the Grid's datacontext to "SideItem", it works, but that changes the whole thing, so it's pretty pointless. Help?

Also, here is the backend (I'm using the Visual Studio 2013 template):

private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
    // TODO: Create an appropriate data model for your problem domain to replace the sample data
    var group = await SampleDataSource.GetGroupAsync((String)e.NavigationParameter);
    this.DefaultViewModel["Group"] = group;
    this.DefaultViewModel["Items"] = group.Items;
    this.DefaultViewModel["SideItem"] = group.Items.First();
}

Answers


Simply set the datacontext of the stack panel:

<StackPanel DataContext="{Binding SideItem}" ...

It's seems SideItem should a property of Group, is that right? What is Group in your code?

EDIT:

If you want to bind to SideItem which is related to a window, try this:

<StackPanel DataContext="{Binding SideItem, RelativeSource={RelativeSource AncestorType={x:Type Window}}}">

Need Your Help

ShareKit -Evernote integration to iPhone App

iphone github social sharekit evernote

I prefered to use the sharekit for the social integration to my iphone app. While configuring the evernote integration when i tested with the default credentails its worked but when I used my own

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.