Databinding between two usercontrols

Jul 14, 2009 at 9:04 AM


I'm having a usercontrol - uc1 which includes another usercontrol uc2.

In uc1 I have an element called myScrollViewer

in uc2 I need to databind to this element

I've tried things like below with no luck so far.


<VisualBrush x:Name="myVisualBrush" <font size="2">



ViewboxUnits="Absolute" <font size="2">



Visual="{Binding Path=myScrollViewer, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ScrollViewer}}}" />


If I have the code directly in uc1, the binding works fine with: Visual="{Binding ElementName=myScrollViewer}"


Any suggestions will be appreciated


Arne Madsen

Jul 15, 2009 at 6:49 AM

That's because named element have a namescope. In this case the scope is the usercontrol. Outside that scope the name is not visible. So, even if uc2 is nested inside uc1, uc2 is also creating its own namescope, meaning that any names outside uc2 are not visible to elements inside uc2.

If you really want this kind of stuff you will have to expose myScrollViewer as a public (dependency) property on uc1.

The RelativeSource binding could have worked if you had left out the Path (and there was not another ScrollViewer in the visual tree in between the point of binding and myScrollViewer)