Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't do that, unfortunately.</p> <p>What you will get immediately is an exception saying a control can have only one logical parent.</p> <p>Although what you can do is to do double-delegation. Your panel delegates measure/arrange to another panel, and in return it provides it with 'ghosts' which will act as a children to it and delegate their own measure/arrange to your panel's children.</p> <p>Here's the code:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Controls; using System.Windows; public class DelegatePanel : Panel { private sealed class DelegateChild : FrameworkElement { readonly Func&lt;Size, Size&gt; measure; readonly Func&lt;Size, Size&gt; arrange; public DelegateChild(Func&lt;Size,Size&gt; measure, Func&lt;Size,Size&gt; arrange) { this.measure = measure; this.arrange = arrange; } protected override Size MeasureOverride(Size availableSize) { return measure(availableSize); } protected override Size ArrangeOverride(Size finalSize) { return arrange(finalSize); } } readonly Dictionary&lt;UIElement, UIElement&gt; delegateByChild = new Dictionary&lt;UIElement,UIElement&gt;(); public Panel LayoutPanel { get { return (Panel)GetValue(LayoutPanelProperty); } set { SetValue(LayoutPanelProperty, value); } } public static readonly DependencyProperty LayoutPanelProperty = DependencyProperty.Register("LayoutPanel", typeof(Panel), typeof(DelegatePanel), new PropertyMetadata(null)); protected override Size MeasureOverride(Size availableSize) { if(this.LayoutPanel==null) return base.MeasureOverride(availableSize); this.delegateByChild.Clear(); this.LayoutPanel.Children.Clear(); foreach (UIElement _child in this.Children) { var child = _child; var delegateChild = new DelegateChild( availableChildSize =&gt; { child.Measure(availableChildSize); return child.DesiredSize; }, finalChildSize =&gt; { return finalChildSize; }); delegateByChild[child] = delegateChild; this.LayoutPanel.Children.Add(delegateChild); } this.LayoutPanel.Measure(availableSize); return this.LayoutPanel.DesiredSize; } protected override Size ArrangeOverride(Size finalSize) { if(this.LayoutPanel==null) return base.ArrangeOverride(finalSize); this.LayoutPanel.Arrange(new Rect(finalSize)); foreach (var kv in delegateByChild) { var child = kv.Key; var delegateChild = kv.Value; var position = delegateChild.TranslatePoint(default(Point), this.LayoutPanel); Rect finalChildBounds = new Rect( position, delegateChild.RenderSize); child.Arrange(finalChildBounds); } return this.LayoutPanel.RenderSize; } } </code></pre> <p>Disclaimer: this doesn't implement VirtualizingPanel. So whilst it does work inside ItemsControl and the gang -- it won't perform quick enough for large collections.</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. This table or related slice is empty.
    1. 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