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}}">
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>

 <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}"/>

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();


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?


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

SIGABRT doesn't terminate Passenger RackApp process

ruby-on-rails ruby apache passenger phusion

Passenger 4.0.59 on RHEL 6.6 managing a Ruby 2.1.5 / Rails 3.2.18 application under Apache 2.2.15.

.NET Applications performance problem on Windows 2003

c# .net performance windows-server

We have a 2 x Quad Core Xeon server with 8GB of RAM and Windows Server 2003 Enterprise installed on it. We installed our application server which is based on .NET Framework 3.5 on it. The server us...