Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>that's because in your code you raise an event on a new UserControl2. for your specific example the code of the UserControl1.button1_Click event should be like this:</p> <pre><code>private void button1_Click(object sender, RoutedEventArgs e) { if (this.Parent != null &amp;&amp; this.Parent is StackPanel) { StackPanel parentControl = this.Parent as StackPanel; foreach (UIElement child in parentControl.Children) { if (child is UserControl2) ((UserControl2)child).CallDelegate(); } } } </code></pre> <p><strong>EDIT:</strong> kay so it seems you want to get all the usercontrol2 within the window1 to be resized. then what you need is to make a recursive function to get the topmost parent, e.g (modded from hardcodet.net/2008/02/find-wpf-parent)</p> <pre><code>DependencyObject GetHighestParent(DependencyObject child) { ContentElement contentElement = child as ContentElement; if (contentElement != null) { DependencyObject parent = ContentOperations.GetParent(contentElement); if (parent != null) return parent; FrameworkContentElement fce = contentElement as FrameworkContentElement; return fce != null ? fce.Parent : null; } FrameworkElement frameworkElement = child as FrameworkElement; if (frameworkElement != null) { DependencyObject parent = frameworkElement.Parent; if (parent != null) { return GetHighestParent(parent); } else { return child; } } DependencyObject visualParent = VisualTreeHelper.GetParent(child); if (visualParent != null) return GetHighestParent(visualParent); else return child; } </code></pre> <p>then you might want to create a method to walkdown all the children like this:</p> <pre><code>void CallDelegateInAllControl2(DependencyObject parent) { int childCount = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i &lt; childCount; i++) { DependencyObject child = VisualTreeHelper.GetChild(parent, i); if (child is UserControl2) { ((UserControl2)child).CallDelegate(); } else { CallDelegateInAllControl2(child); } } } </code></pre> <p>and then you call it within button1_click event</p> <pre><code>private void button1_Click(object sender, RoutedEventArgs e) { DependencyObject parent = GetHighestParent(this); if(parent!=null) CallDelegateInAllControl2(parent); } </code></pre> <p>note: a walk to get parent and child might be tricky and risky i think and i believe it's a long process so you might just want to re-layout your window1 so it has a StackPanel/Grid with a usercontrol1 element and all usercontrol2 elements within it so you can use the first code i post.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload