MKII
MKII

Reputation: 911

Binding to an UserControl's property in a DataTemplate

I have a simple data template in an UserControl composed of a TextBlock and two buttons. The DataContext is set to a list of objects. One button and the TextBlock are bound to properties of that object, but I need one button to be bound to a property of the UserControl.

Can I use RelativeSource to try and find the UC's class, and get the property that way? Something like this:

{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type MyUserControlClass}}, Path=SomeProperty} 

Upvotes: 0

Views: 77

Answers (1)

PlantPorridge
PlantPorridge

Reputation: 185

At the top of the UserControl give it:

x:Name="MyUserControl"

Then to access the property you can simply use:

"{Binding ElementName=MyUserControl, Path=SomeProperty}"

Upvotes: 1

Related Questions