附加属性不返回值
我为自定义面板声明了一个附加属性:
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>
但如果我将此面板用作 ListBox
的 ItemsPanelTemplate
,它总是返回 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>
我还注意到,当在 ListBox 中使用自定义环绕面板时,它会在 Arrange 方法中发送 double.PositiveInfinite ,因此 Arrange 永远无法设置值。单独使用时效果相同,
谢谢
I have an attached property declared for my custom panel as:
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);
}
It works fine if I define the panel as:
<local:WeightedPanel Grid.Row="0" Height="200">
<Button local:WeightedPanel.Weight="8" />
<Button local:WeightedPanel.Weight="2"/>
</local:WeightedPanel>
But if I use this panel as ItemsPanelTemplate
for a ListBox
, it always returns the default value in 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>
I also noticed that when the custom wrap panel is used in a ListBox
, it sends double.PositiveInfinite
in Arrange method and therefore Arrange is never able to set the values. The same works fine when used all by itself
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即使我尝试了相同的方法,但它在其他面板形式中对我不起作用,我想设置网格,但它不起作用。
问题是,由于 ListBox 只能将 ListBoxItem 作为其真正的逻辑子项,而不是任何 Button 等,因此当您在 ListBox 的内容窗格中添加 Button 或任何项目时,当它执行时,ItemsPanel 将具有其直接子项,因为 ListBoxItem 和 ListBoxItem 的内容将是您添加的控件。
因此,在运行时,这将是您的视觉树...
这就是您的附加属性不起作用的原因。
解决方案是,您尝试将 ItemContainerStyle 中的 ListBoxItem 的属性设置为 DataContext 的 WeightedPanel.Weight。我知道这很令人困惑。
或者
您可以将 ListBoxItem 添加为子项.. 就像
Even i tried same and it didnt work for me in some other panel form, I wanted to setup grid but it didnt work.
The problem is, since ListBox can have only ListBoxItem as its real logical child, not any Button etc, when you add Button or any item in ListBox's content pane, when it executes, the ItemsPanel will have its immediate children as ListBoxItem and ListBoxItem's content will be the control you added.
So at runtime this will be your visual tree...
This is the reason your attached property is not working.
The solution would be, you try to set ListBoxItem's property in the ItemContainerStyle to be of DataContext's WeightedPanel.Weight. I know its confusing.
OR
You can add ListBoxItem as child.. like