DaveO
DaveO

Reputation: 1989

Set inherited Binding source for UserControl element

Simple syntax question really but I'm getting nowhere:

(Psuedo code)

MainWindow.xaml:

<grid>
  <control:MyUserControl DataContext="{StaticResource MyDataSource}" />
</grid>

MyUserControl.xaml

<grid>
  <stackpanel DataContext="{StaticResource MyOtherDataSource}" IsEnabled="{Binding Path=CanUseMe, Source={StaticResource MyDataSource}" />
</grid>

The problem is {StaticResource MyDataSource} in the stackpanel, since the user control doesn't have this resource.

How do I set the source binding to be the 'global' datasource passed into the usercontrol when I've already set the DataContext for the stackpanel?

Thanks!

Upvotes: 1

Views: 426

Answers (1)

Fredrik Hedblad
Fredrik Hedblad

Reputation: 84657

The UserControl's DataContext will be MyDataSource, so you can use RelativeSource in the Binding like this

<grid>
    <stackpanel DataContext="{StaticResource MyOtherDataSource}"
                IsEnabled="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}},
                                    Path=DataContext.CanUseMe}"/>
</grid> 

Upvotes: 1

Related Questions