我为我的自定义面板声明了一个附加属性:
public static readonly DependencyProperty WeightProperty = DependencyProperty.RegisterAttached(
"Weight", typeof(double), typeof(WeightedPanel),
new FrameworkPropertyMetadata(1.0,
FrameworkPropertyMetadataOptions.AffectsParentMeasure |
FrameworkPropertyMetadataOptions.AffectsParentArrange ));
public static void SetWeight(DependencyObject obj, double weight)
{
obj.SetValue(WeightProperty, weight);
}
public static double GetWeight(DependencyObject obj)
{
return (double) obj.GetValue(WeightProperty);
}
如果我将面板定义为:
<local:WeightedPanel Grid.Row="0" Height="200">
<Button local:WeightedPanel.Weight="8" />
<Button local:WeightedPanel.Weight="2"/>
</local:WeightedPanel>
但是,如果我将此面板用作ItemsPanelTemplate
a ListBox
,它总是返回默认值ArrangeOverride
。
<ListBox Grid.Row="2" Height="100">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<local:WeightedPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<Button local:WeightedPanel.Weight ="6" />
<Button local:WeightedPanel.Weight ="4"/>
</ListBox>
我还注意到,当在 a 中使用自定义包装面板时ListBox
,它会发送double.PositiveInfinite
Arrange 方法,因此 Arrange 永远无法设置值。单独使用时同样有效
谢谢