UserControl中的Windows Phone UIElementCollection

时间:2013-02-12 09:41:46

标签: windows-phone-7 windows-phone-8 windows-phone

我的UserControl中有一个带有自定义UIElements的UIElementCollection。但是没有显示UIElements。怎么了?

public partial class MyControl : UserControl
{
    public static readonly DependencyProperty ChildrenProperty = DependencyProperty.Register("Children", typeof(UIElementCollection), typeof(MyControl), null);

    public UIElementCollection Children
    {
        get { return (UIElementCollection)GetValue(ChildrenProperty); }
        set { SetValue(ChildrenProperty, value); }
    }

    protected override Size MeasureOverride(Size availableSize)
    {
        Children.Clear();
        UserControl child = new SecondUserControl();
        child.Measure(availableSize);
        Children.Add(child);
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        foreach (UIElement child in Children)
        {
            child.Arrange(new Rect(0,0, child.DesiredSize.Width, child.DesiredSize.Height));
        }
        return finalSize;
    }
}

1 个答案:

答案 0 :(得分:0)

Oy vey。无论你想做什么,这都不是做到这一点的方法。

首先,您的儿童收藏品永远不会进入用户界面。只是声明属性和测量内容实际上并没有将它添加到UI。您必须通过将子项添加到UserControl.Content上的某些内容来确保子项是视觉t​​re的一部分。

其次,为什么要在每个测量通道上添加和删除子项?这对任何用例都没有意义。措施&安排通过不是删除/添加控制的时间,是时候测量和安排控制。

相关问题