1

Say I have a user control like the one below, how would I bind something to the ActualWidth of the "G1" grid from outside of the control?

<UserControl x:Class="Blah">
  <WrapPanel>
    <Grid x:Name="G1">
      ...
    </Grid>
    <Grid>
      ...
    </Grid>
  </WrapPanel>
</UserControl>
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
MJS
  • 155
  • 3
  • 9

2 Answers2

2

If you mean with outside the control, not as Content of the control, you can use ElementName in the Binding like so:

{Binding ElementName=G1, Path=ActualWidth}

If you mean outside the control in another Xaml file, then you can try to use the Path property if your control is in the scope of the other control:

{Binding ElementName=ParentControl, Path=G1.ActualWidth}

However I would advise against this design, because you may change the name of G1 one day, and you would never know of any bindings that might break.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Arcturus
  • 26,677
  • 10
  • 92
  • 107
  • {Binding ElementName=ParentControl, Path=G1.ActualWidth} doesn't work & the control is in scope - I can see the actual width of the whole control. – MJS Nov 24 '08 at 00:58
1

If you want to bind to an external control where you use this user control, declare a DependencyProperty at your UserControl code behind and then Bind G1 to that property. And bind the external control's property to the UserControl's DependencyProperty. It is like a 2 level of indirection.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Jobi Joy
  • 49,102
  • 20
  • 108
  • 119