添付プロパティをコード ビハインドで動作させようとしていますが、明らかに何かが欠けています。私が理解しているように、結果は「テスト」である必要がありますが、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
{
}