wpf 更改RenderTransform调用ArrangeOverride,即使它不应该[关闭]

rbl8hiat  于 2023-01-27  发布在  其他
关注(0)|答案(1)|浏览(172)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
2天前关闭。
Improve this question
我有一个自定义面板Foo,它有另一个自定义面板栏作为它的孩子。
当用户移动鼠标时,Foo调用Bar上的方法'Update',使bar将其renderTransform设置为不同的值。
当我这样做的时候,Bar中的ArrangeOverride方法被调用了(不是在Foo上),我很困惑为什么会发生这种情况,因为不需要进行布局更改。这是故意的还是某种bug?

yyhrrdl8

yyhrrdl81#

如果你看一下UIElement.Rendertransform的参考源代码。
https://referencesource.microsoft.com/#q=rendertransform

[CommonDependencyProperty]
    public static readonly DependencyProperty RenderTransformProperty =
                DependencyProperty.Register(
                            "RenderTransform",
                            typeof(Transform),
                            typeof(UIElement),
                            new PropertyMetadata(
                                        Transform.Identity,
                                        new PropertyChangedCallback(RenderTransform_Changed)));

    /// <summary>
    /// The RenderTransform property defines the transform that will be applied to UIElement during rendering of its content.
    /// This transform does not affect layout of the panel into which the UIElement is nested - the layout does not take this
    /// transform into account to determine the location and RenderSize of the UIElement.
    /// </summary>
    public Transform RenderTransform
    {
        get { return (Transform) GetValue(RenderTransformProperty); }
        set { SetValue(RenderTransformProperty, value); }
    }

    private static void RenderTransform_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        UIElement uie = (UIElement)d;

        //if never measured, then nothing to do, it should be measured at some point
        if(!uie.NeverMeasured && !uie.NeverArranged)
        {
            // If the change is simply a subproperty change, there is no
            //  need to Arrange. (which combines RenderTransform with all the
            //  other transforms.)
            if (!e.IsASubPropertyChange)
            {
                uie.InvalidateArrange();
                uie.AreTransformsClean = false;
            }
        }
    }

更改已进行测量排列的uireelement的属性将在该uireelement上调用InvalidateArrange。
这将导致安排被调用。
我们对您的代码了解不够,无法提出具体的更好方法,但这不是问题所在。您需要确保更改了子属性,或者确保调用ArrangeOverride无关紧要,或者(我猜不太实用)在更改之前避免父面板中的布局过程。
也许你可以在Bar中添加一个IgnoreArrange标志。默认为false。在改变变换之前设置为true,之后设置为false,如果为true,则返回ArrangeOverride。
底线是,这是上面的代码导致你所看到的。

相关问题