gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:27:24 24 4
gpt4 key购买 nike

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

最佳答案

这证明(或可以证明)它没有:

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();
}
}

关于wpf - 不,Adorner 不会在 WPF 中自动获取其 AdornedElement 的 DataContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/764682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com