如何做一个容器知道当一个孩子已呼吁InvalidateArrange?(How does a con

2019-08-01 16:50发布

我努力学习创造在WinRT的XAML应用程序的自定义面板的基本知识。 我已经定义了一个附加的依赖属性,它的工作如预期,除了我无法弄清楚如何获得财产的回调子元素触发安排或容器的措施。

什么是对孩子让它的集装箱知道,安排和措施应重新调用的正确方法? 在我的WPF 4释放出的书,他们使用FrameworkPropertyMetadataOptions.AffectsParentArrange但是这似乎并没有在WinRT中可用。

public class SimpleCanvas : Panel
{
    #region Variables
    #region Left Property
    public static double GetLeft(UIElement element)
    {
        if (element == null)
        {
            throw new ArgumentNullException("element");
        }

        object value = element.GetValue(LeftProperty);
        Type valueType = value.GetType();
        return Convert.ToDouble(value);
    } 

    public static void SetLeft(UIElement element, double value)
    {
        if (element == null)
        {
            throw new ArgumentNullException("element");
        }

        element.SetValue(LeftProperty, value);
    }

    public static readonly DependencyProperty LeftProperty =
        DependencyProperty.RegisterAttached("Left", typeof(double), typeof(SimpleCanvas),
        new PropertyMetadata(0, OnLeftPropertyChanged));

    public static void OnLeftPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
        UIElement element = (UIElement)source;
        // This doesn't cause ArrangeOverride below to be called
        element.InvalidateArrange();
    }
    #endregion

    #region Top Property
    public static double GetTop(UIElement element)
    {
        if (element == null)
        {
            throw new ArgumentNullException("element");
        }

        object value = element.GetValue(TopProperty);
        return (value == null) ? 0 : (double)value;
    }

    public static void SetTop(UIElement element, double value)
    {
        if (element == null)
        {
            throw new ArgumentNullException("element");
        }

        element.SetValue(TopProperty, value);
    }

    public static readonly DependencyProperty TopProperty =
        DependencyProperty.RegisterAttached("Top", typeof(double), typeof(SimpleCanvas),
        new PropertyMetadata(0, OnTopPropertyChanged));

    public static void OnTopPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
        UIElement element = (UIElement)source;
        // This doesn't cause ArrangeOverride below to be called
        element.InvalidateArrange();
    }
    #endregion
    #endregion

    public SimpleCanvas()
    {

    }

    #region Methods
    protected override Size MeasureOverride(Size availableSize)
    {
        foreach (UIElement child in this.Children)
        {
            child.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
        }

        return new Size(0, 0);
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        foreach (UIElement child in this.Children)
        {
            double x = 0;
            double y = 0;

            double left = GetLeft(child);
            double top = GetTop(child);

            if (!double.IsNaN(left))
            {
                x = left;
            }
            if (!double.IsNaN(top))
            {
                y = top;
            }

            child.Arrange(new Rect(new Point(x, y), child.DesiredSize));
        }

        return finalSize;
    }
    #endregion
}

Answer 1:

我迟到了,但我去同一个方向,面临同样的问题。 这里是我的解决方案。

在回调调用InvalidateArrange你连接你属性的子元素:

public static void OnTopPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
    UIElement element = (UIElement)source;
    // This doesn't cause ArrangeOverride below to be called
    element.InvalidateArrange();
}

但是,你真的应该无效的面板,通过改变你的代码,以便:

public static void OnTopPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
    UIElement panel= VisualTreeHelper.GetParent(source) as UIElement;
    if(panel != null)       
        panel.InvalidateArrange();
}

它应该工作(为我做了)。



Answer 2:

如果InvalidateArrange单独不工作,你也可以尝试InvalidateMeasure或UpdateLayout请。



Answer 3:

我有这个问题与依赖于子控件FontSize父控件。 我解决了旅行了父母的堆栈和无效的一切问题:

    static MyControl()
    {
        // replace base implementation of the dependent property 
        FontSizeProperty.OverrideMetadata(typeof(Scalar), 
            new FrameworkPropertyMetadata(SystemFonts.MessageFontSize, FrameworkPropertyMetadataOptions.Inherits, OnMeasureInvalidated));
    }

    private static void OnMeasureInvalidated(DependencyObject sender, DependencyPropertyChangedEventArgs args)
    {
        // recurse over parent stack
        while (true)
        {
            var parent = VisualTreeHelper.GetParent(sender) as UIElement;
            if (parent == null) return; // break on root element
            parent.InvalidateMeasure();
            sender = parent;
        }
    }


文章来源: How does a container know when a child has called InvalidateArrange?