当我更改这些DependencyProperty值时,为什么设计器不更新?

时间:2016-10-03 21:51:49

标签: c# wpf

我使用StartPosition LayoutDirection依赖项属性扩展了this example for a RadialPanel

public class RadialPanel : Panel
{
    public RadialPanel()
    {
        StartPosition = StartPosition.Top;
        LayoutDirection = LayoutDirection.Clockwise;
    }

    public StartPosition StartPosition
    {
        get { return (StartPosition)GetValue(StartPositionProperty); }
        set { SetValue(StartPositionProperty, value); }
    }
    public static readonly DependencyProperty StartPositionProperty =
        DependencyProperty.Register("StartPosition", typeof(StartPosition), typeof(RadialPanel), new PropertyMetadata(StartPosition.Top));


    public LayoutDirection LayoutDirection
    {
        get { return (LayoutDirection)GetValue(LayoutDirectionProperty); }
        set { SetValue(LayoutDirectionProperty, value); }
    }
    public static readonly DependencyProperty LayoutDirectionProperty =
        DependencyProperty.Register("LayoutDirection", typeof(LayoutDirection), typeof(RadialPanel), new PropertyMetadata(LayoutDirection.Clockwise));

// ... code from cited article
}

StartPositionLayoutDirection值是枚举:

public enum StartPosition
{
    Left = 180, Top = 90, Right = 0, Bottom = -90
}

public enum LayoutDirection
{
    Clockwise = -1, CounterClockwise = 1
}

数学运算正常,当我构建项目时,所有内容都会刷新,但我认为使用依赖项属性会在我更改XAML中的值时自动更新设计器:

<local:RadialPanel StartPosition="Top" 
                   LayoutDirection="CounterClockwise" >

......但事实并非如此。当我更改RadialPanel的Children时它会更新,但就是这样。

如何启用此功能?

1 个答案:

答案 0 :(得分:1)

默认情况下,WPF不会在每次更改属性后重新计算控件的布局。您需要告诉框架,给定属性会影响布局。

在两个属性上使用FrameworkPropertyMetadata而不是FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectArrange而不是PropertyMetadata,并且框架将在Panel更改后运行测量/排列。