简体   繁体   中英

How can I access the parent usercontrols elements from the child user control in wpf?

如何从wpf的子用户控件访问父usercontrol的元素?

Using VisualTreeHelper you can find Parent control's by GetParent() method. If you want to find parent control in Templates you can take help of RelativeSource FindAncestor .

If you need those elements to get their values, eg for binding, you can use this construction (here the background of the element will be using the values of its parent):

Background="{Binding Path=BorderBrush, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}}

In the code, for every element extending FrameworkElement class, there is Parent property, just cast it to the correct type (I assume you know, what type it is) and you're done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM