Wednesday, August 15, 2012

Cannot bind to TabItem DataContext from the TabItem Content in Silverlight

Cannot bind to TabItem DataContext from the TabItem Content in Silverlight

I have a TabItem that has it's DataContext set to a ViewModel. The header is set to a header View and the Content is set to an item view

However this Binding from the item view just doesn't work

<TextBox Margin="0" Grid.Column="0" Grid.Row="0" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=sdk:TabItem, AncestorLevel=1}, Path=DataContext.Job.SourceCode, Mode=TwoWay}"/> 

yet this binding

<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=sdk:TabItem, AncestorLevel=1}, Path=DataContext.Job.Name}"/> 

which is in the header view works fine.

I've even tried putting the TextBlock from the Header into the Item and setting the Content to a new HeaderView. No matter what I try I can't get the bindings from the Content to work even though the header binding works fine

Can anyone explain?

Adding the full Views as requested first the Item

<UserControl x:Class="IE.Intergration.JobBuilder.Views.TabbedJobs.TabbedJobItem" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"      DataContext="{Binding Source={StaticResource ViewModelLocator}, Path=New.TabbedJobs.TabbedJobItem}"     > <UserControl.Resources> </UserControl.Resources> <Grid x:Name="LayoutRoot" >     <Grid.ColumnDefinitions>     </Grid.ColumnDefinitions>     <Grid.RowDefinitions>         <RowDefinition Height="*"  />         <RowDefinition Height="40"/>     </Grid.RowDefinitions>      <TextBox Margin="0" Grid.Column="0" Grid.Row="0" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=sdk:TabItem, AncestorLevel=1}, Path=DataContext.Job.SourceCode, Mode=TwoWay}"/>     <StackPanel Grid.Column="0" Grid.Row="1" FlowDirection="LeftToRight" Orientation="Horizontal">         <Button Content="Save" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=sdk:TabItem, AncestorLevel=1}, Path=DataContext.Save}" />      </StackPanel> </Grid> 

and the Header

<UserControl x:Class="IE.Intergration.JobBuilder.Views.TabbedJobs.TabbedJobItemHeader" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"     DataContext="{Binding Source={StaticResource ViewModelLocator}, Path=New.TabbedJobs.TabJobHeader}"      >  <Grid x:Name="LayoutRoot">     <TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=sdk:TabItem, AncestorLevel=1}, Path=DataContext.Job.Name}"/> </Grid> 

Answers & Comments...




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog