usefulBee
usefulBee

Reputation: 9692

How to Expose Properties of a Control that is Inside a User Control?

For example, I have a button that is inside a User Control, and I would like to expose its own properties, so whenever somebody is using the User Control could easily change its button's properties values. I was actually able to accomplish this in two ways:

First, Bind the Button's property such as, FontSize to its equivalent in the UserControl:

<Button FontSize="{Binding Path=FontSize, ElementName=UserControl}" />

Second, Bind the Button's property to a new Dependency Property that is added to the User Control.

<Button Content="{Binding Path=DependencyProperty, ElementName=UserControl}"

The question now, is there any way to access the Button's properties directly in XAML without doing any bindings to the User Control?

Upvotes: 1

Views: 599

Answers (1)

michal.ciurus
michal.ciurus

Reputation: 3664

It's not really desired to name Controls in WPF. What you can do is use binding on the Parent using it's type rather than naming it.

<Button FontSize="{Binding FontSize, RelativeSource={RelativeSource Mode=FindAncestor, 
                   AncestorType={x:Type UserControl}}}" />

It's the easiest way.

Upvotes: 2

Related Questions