简体   繁体   English

在控件生命周期的哪个阶段,它可以访问继承的附加属性?

[英]At what stage in a control's lifetime can it access inherited attached properties?

I have a control that uses a certain inherited attached property, and I found that I can't access that property in the constructor, which is reasonable, since at that stage the control isn't a part of the visual tree and so it can't inherit any attached properties. 我有一个使用某些继承的附加属性的控件,但发现无法在构造函数中访问该属性,这是合理的,因为在那个阶段,该控件不是可视化树的一部分,因此可以不会继承任何附加属性。

// Constructor
public MyClassName()
{
    InitializeComponent();

    MyValue value = DeclaringClass.GetMyAttachedProperty(depObj); // value == null
}

When I try to access the property during the Loaded event, I am able to retrieve the value: 当我尝试在Loaded事件期间访问属性时,我能够检索该值:

// Constructor
public MyClassName()
{
    InitializeComponent();

    Loaded += OnLoaded;
}

void OnLoaded(object sender, RoutedEventArgs e)
{
    Loaded -= OnLoaded;

    MyValue value = DeclaringClass.GetMyAttachedProperty(depObj); // value != null

    // Do something with value
}

So this brings me to my question - what is the earliest stage in the control's lifetime where it can access inherited attached properties? 因此,这使我想到了一个问题-控件生命周期中最早的哪个阶段可以访问继承的附加属性? Is there a better place to do so instead on the Loaded event? Loaded事件上是否有更好的方法呢?

The earliest possible is indeed inside the Loaded event handler. 最早可能确实在Loaded事件处理程序内部。 Your only other choice is the constructor (or the morally equivalent Initialized event) but that's too soon for dependency properties to assume inherited values. 唯一的选择是构造函数(或在道德上等效的Initialized事件),但是对于依赖属性而言,现在假定继承值还为时过早。

See also common object lifetime events at MSDN. 另请参见MSDN上的常见对象生存期事件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从继承的用户控件访问 viewModel 依赖属性? - how to access to viewModel dependency properties from inherited user control? 如何在没有访问权限的情况下覆盖继承控件的事件? - How to override an inherited control's event without having access to it? C#无法访问类的基本属性(仅继承) - C# Can't access base properties of a class (only inherited) 从UserControl的属性访问内部控件的所有属性 - Access from UserControl's properties to all of a inside control's properties 是否可以在没有附加FileUpload控件的文本框的情况下上传文件? - Can I upload a file without the FileUpload control's attached textbox? 无法访问用户控件的属性 - Can't access the properties of user control C# 如何“一般地”将 UI 控件引用传递给 class 模块并访问它的属性? - C# How can I pass a UI control reference to a class module "generically" and access it' s properties? 我可以用什么WPF方法在控件的第一个显示器上设置一些属性? - what WPF method can I use to set some properties on a control's first display? 如何允许在继承的控件(按钮)C#winforms的构造函数中初始化的属性进行修改 - How can I allow for the modifications of properties that are initialized in the constructor of an inherited control (button) C# winforms 如何隐藏ASP.NET自定义控件的继承属性? - How can I hide inherited properties of an ASP.NET custom control?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM