简体   繁体   English

UserControl中的Windows Phone UIElementCollection

[英]Windows Phone UIElementCollection in UserControl

I have a UIElementCollection in my UserControl with custom UIElements. 我的UserControl中有一个带有自定义UIElements的UIElementCollection。 But the UIElements are not displayed. 但是不会显示UIElement。 Whats wrong? 怎么了?

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;
    }
}

Oy vey. OY合租。 Whatever it is you're trying to do, that isn't the way to do it. 无论您要做什么,这都不是做到的方式。

First, your Children colleciton never makes it to the UI. 首先,您的Children大学生永远不会进入UI。 Just declaring properties and measuring stuff doesn't actually add it to the UI. 仅声明属性和度量内容实际上并不会将其添加到UI中。 You have to make sure the children are part of the visual tre by adding them to something on the UserControl.Content. 您必须通过将子级添加到UserControl.Content上的某些内容来确保它们是视觉tre的一部分。

Second, why are you adding and removing children on every Measure pass? 其次,为什么要在每个Measure通道中添加和删除子级? That doesn't make sense for any usecase. 这对于任何用例都没有意义。 The Measure & arrange pass isn't a time to remove/add control, it's a time to measure and arrange control. “测量和排列”通过不是删除/添加控件的时间,而是测量和排列控制的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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