Matvey
Matvey

Reputation: 163

WPF Set usercontrol property from outside

My user control has a slider. I use this control in some view and want to assign the Value property of it from outside. Something like this:

<uc:MyUserControl VerticalAlignment="Bottom" Margin="8,8,8,0" slider.Value="{Binding...}"/>

What is the syntax to do this ?

Thank you :)

Upvotes: 1

Views: 331

Answers (2)

Thermonuclear
Thermonuclear

Reputation: 352

Depending on how complex the UserControl is you could just bind it's DataContext to the same one you're using for the parent control or alternatively host the viewmodel for the UserControl within the viewmodel of the parent control. Depending on your situation this may not be the most 'elegant' solution but if your UserControl is strictly UI then I believe this is the best solution otherwise go with Mike's answer.

Upvotes: 0

Mike Strobel
Mike Strobel

Reputation: 25623

There is no syntax to externally reference a control within a UserControl; your UserControl is a black box, and nobody on the outside should need to know its structure.

If you need to provide a property when instantiating your UserControl, declare a new DependencyProperty on the UserControl, and then bind your slider's Value to that property.

Upvotes: 2

Related Questions