不,Adorner 不会自动获取 WPF 中其 AdornedElement 的 DataContext

发布于 2024-07-17 12:28:11 字数 78 浏览 11 评论 0原文

原始问题:Adorner 是否会自动继承 WPF 中其“AdornedElement”的“DataContext”?

Original question: Does an Adorner automagically inherit the "DataContext" of its "AdornedElement" in WPF?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肤浅与狂妄 2024-07-24 12:28:11

这证明(或可以证明)它不:

public class SomeObject
{ }

public class SomeAdorner : Adorner
{
    public SomeAdorner(UIElement adornedElement) : base(adornedElement)
    {
        // comment out the following statement to see that, by default, an adorner does not
        // take on the data context of its adorned ui element
        SetBinding(
            DataContextProperty,
            new Binding(DataContextProperty.Name)
            {
                Mode = BindingMode.OneWay,
                Source = adornedElement
            }
        );
    }

    protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
    {
        base.OnPropertyChanged(e);

        if ((e.Property.Name.Equals(DataContextProperty.Name)) && (e.NewValue is SomeObject))
        { MessageBox.Show("DataContext changed!"); }
    }
}

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(Window1_Loaded);
    }

    void Window1_Loaded(object sender, RoutedEventArgs e)
    {
        AdornerLayer.GetAdornerLayer(WindowContentWithElementName)
            .Add(new SomeAdorner(WindowContentWithElementName));

        WindowContentWithElementName.DataContext = new SomeObject();
    }
}

This proves (or can prove) that it doesn't:

public class SomeObject
{ }

public class SomeAdorner : Adorner
{
    public SomeAdorner(UIElement adornedElement) : base(adornedElement)
    {
        // comment out the following statement to see that, by default, an adorner does not
        // take on the data context of its adorned ui element
        SetBinding(
            DataContextProperty,
            new Binding(DataContextProperty.Name)
            {
                Mode = BindingMode.OneWay,
                Source = adornedElement
            }
        );
    }

    protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
    {
        base.OnPropertyChanged(e);

        if ((e.Property.Name.Equals(DataContextProperty.Name)) && (e.NewValue is SomeObject))
        { MessageBox.Show("DataContext changed!"); }
    }
}

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(Window1_Loaded);
    }

    void Window1_Loaded(object sender, RoutedEventArgs e)
    {
        AdornerLayer.GetAdornerLayer(WindowContentWithElementName)
            .Add(new SomeAdorner(WindowContentWithElementName));

        WindowContentWithElementName.DataContext = new SomeObject();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文