我使用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
}
StartPosition
和LayoutDirection
值是枚举:
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时它会更新,但就是这样。
如何启用此功能?
答案 0 :(得分:1)
默认情况下,WPF不会在每次更改属性后重新计算控件的布局。您需要告诉框架,给定属性会影响布局。
在两个属性上使用FrameworkPropertyMetadata
而不是FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectArrange
而不是PropertyMetadata
,并且框架将在Panel
更改后运行测量/排列。