【发布时间】:2013-04-24 05:08:50
【问题描述】:
我正在尝试让附加属性在后面的代码中工作,但我显然遗漏了一些东西。据我了解,结果应该是“测试”,但它是 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
{
}
【问题讨论】:
标签: wpf code-behind attached-properties