问题描述
我无法将附加属性值从树中从父级传播到可视层次结构中的子级。设置如下:
I am having trouble getting an attached property value to propagate down the tree from a parent to a child in the visual hierarchy. The setup is as follows:
我有一个实例化Viewport3D的自定义面板。然后Panel处理添加和删除的子项,为每个子项创建和添加一个继承的Visual3D类。
I have a custom panel that instantiates a Viewport3D. The Panel then handles the child added and removed to create and add an inherited Visual3D class for each child item.
我试图声明一个名为AttachedToggle属性的附加属性。我希望将此属性驻留在名为AttachedToggle的外部所有者类中,该类实现单个附加依赖项属性IsChecked,并允许父Panel或任何子Visual3D元素能够更改值并具有其他元素的实例价值反映了变化。 Panel和Visual3D类都不是从公共基础继承的。可以这样做吗?
I am trying to declare an attached property called AttachedToggle property. I would like to have this property reside in an external owner class called AttachedToggle which implements the single attached dependency property IsChecked and allow either the parent Panel or any of the children Visual3D elements to be able to change the value and have the other elements' instance values reflect the change. Neither the Panel nor the Visual3D class inherit from a common base. Can this be done?
我可以使用Set / GetValue从应用程序代码成功更改IsChecked的父实例和子实例值,但无法进行传播更改。
I can successfully change both the parent and child instance value of IsChecked from application code using Set/GetValue but am unable to get the change to propagate.
推荐答案
根据。确保您注册该属性的调用如下所示:
This should work fine, according to the docs on Property Value Inheritance. Make sure that your call to register the property looks like:
public static readonly DependencyProperty IsChecked =
DependencyProperty.RegisterAttached(
"IsChecked",
typeof(Boolean),
typeof(AttachedToggle),
new FrameworkPropertyMetadata(false,
FrameworkPropertyMetadataOptions.Inherits)
);
- 继承
标志。请参阅(和 - 您也可以使用 OverridesInheritanceBehavior
(如果需要)。
-Note the Inherits
flag. See the docs for FrameworkPropertyMetadata
(and FrameworkPropertyMetadataOptions
- you can use OverridesInheritanceBehavior
if needed ) as well.
这篇关于获取附加属性继承以进行传播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!