我试图让附加属性在后面的代码中工作,但我显然遗漏了一些东西。据我了解,结果应该是“测试”,但它是 string.Empty。
LogicalTreeHelper 声明它child
是 的子parent
节点,因此树的设置正确。
有什么建议么?
public partial class MainWindow : Window
{
public MainWindow()
{
var parent = new TestParent();
var child = new Child();
parent.AddLogicalChild(child);
parent.SetValue(TestParent.TestProperty, "test");
var result = child.GetValue(TestParent.TestProperty); // Returns ""
InitializeComponent();
}
}
class TestParent : FrameworkElement
{
public static readonly DependencyProperty TestProperty =
DependencyProperty.RegisterAttached("Test", typeof(string), typeof(TestParent),
new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.Inherits));
public void AddLogicalChild(FrameworkElement element)
{
base.AddLogicalChild(element);
}
}
class Child : FrameworkElement
{
}