New Dev
New Dev

Reputation: 49590

Reference ViewModel's DataContext from within a DataTemplate in a DataTemplate

I found myself in a bit of a bind... with bindings... hehe... (lame)

Anyway... I need to refer to the main ViewModel's Property, but from within a DataTemplate, which is itself within another DataTemplate... and it looks like the only RelativeSource mode that Silverlight allows are: Self and TemplatedParent. And TemplatedParent doesn't go up enough.

Is there anything that can be done?

Some code:

<phone:PanoramaItem>
    <phone:LongListSelector Margin="0,-38,-22,2" ItemsSource="{Binding Items}">
        <phone:LongListSelector.ItemTemplate>
            <DataTemplate>
                <common:ItemContentTemplateSelector DataContext="{Binding}" Content="{Binding ItemContent}" HorizontalContentAlignment="Stretch" Margin="12,2,0,4">
                    <common:ItemContentTemplateSelector.DefaultTemplate>
                        <DataTemplate>
                            .....
                        </DataTemplate>
                    </common:ItemContentTemplateSelector.DefaultTemplate>
                    <common:ItemContentTemplateSelector.PhoneNumberTemplate>
                        <DataTemplate>
                            <Grid Background="White" Height="102">
                                <Border x:Name="border">
                                    <TextBlock Text="Call"/>
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="MouseLeftButtonDown">
                                     <!-- BINDING ERROR -->
                                            <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=DataContext.PhoneCallCommand}"/>
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                </Border>
                            </Grid>
                        </DataTemplate>
                    </common:ItemContentTemplateSelector.PhoneNumberTemplate>
                </common:ItemContentTemplateSelector>
            </DataTemplate>
        </phone:LongListSelector.ItemTemplate>
    </phone:LongListSelector>
</phone:PanoramaItem>

Upvotes: 1

Views: 663

Answers (1)

Davut G&#252;rb&#252;z
Davut G&#252;rb&#252;z

Reputation: 5716

If you don't wont a workaround or make it easily, There is something to do;

Using StaticResource

have a look at this;

   MyViewModelClass ViewModel
   {
        get;set;
   }

  ...ctor()
  {
     this.ViewModel=new MyViewModelClass();
     this.DataContext=this.ViewModel; //We use same VM instance as DataContext as Resource
     this.Resoureces.Add("MainVieModel",ViewModel);
     InitializeComponents();//...Add resource before this line
  }

After that you can use your ViewModel at the same time as a StaticResource anywhere you want;

{Binding Path=Items,Source={StaticResource ViewModel}}

I coded on stackoverflow's editor. There could be some missing chars ...

Upvotes: 3

Related Questions