Linking to an element in a UserControl

Let's say I have a custom control like the one below, how would I bind something to ActualWidth

the "G1" grid out of control?

<UserControl x:Class="Blah">
  <WrapPanel>
    <Grid x:Name="G1">
      ...
    </Grid>
    <Grid>
      ...
    </Grid>
  </WrapPanel>
</UserControl>

      

+1


source to share


2 answers


If you want to bind to an external control where you are using this custom control, declare a DependencyProperty

UserControl in your code and then bind G1 to this property. And bind the external control property to UserControl DependencyProperty

. This is similar to level 2 indirection.



+1


source


If you mean outside the outer control and not as the content of the control, you can use ElementName

in Binding like so:

{Binding ElementName=G1, Path=ActualWidth}

      

If you mean an external control in another Xaml file, you can try using the Path property if your control is in the scope of another control:



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

      

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

+1


source







All Articles