如何从方法读取依赖属性
XAML:
<my:Control ItemsSource="{StaticResource MySource}" A="true" />
假设一个 Control 具有依赖属性 A
,默认值为 false
; 以及处理源集合的方法:
protected override void OnItemsSourceChanged(System.Collections.IEnumerable oldValue, System.Collections.IEnumerable newValue) {}
您希望在其中查看 A
并读出其值(这是正确的)。 您如何确保 A
已初始化并具有给定值?
或者应该如何正确地完成此操作?
在我的例子中,A类似于AllowLateBinding..
强制回调可以帮助我吗?
XAML:
<my:Control ItemsSource="{StaticResource MySource}" A="true" />
Assume a Control with a dependency property A
with a default value false
;
and a method to handle the Source Collection:
protected override void OnItemsSourceChanged(System.Collections.IEnumerable oldValue, System.Collections.IEnumerable newValue) {}
in which you want to look at A
and readout its value (which is true).
how would you ensure, that A
is already initialized and has a given value?
Or how should this be done correctly ?
In my case A is something like AllowLateBinding ..
Could coerce callback help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定正确性,但根据您的确切程序逻辑,这可能有效:
I am not sure about the correctness but depending on your exact program logic, this might work:
您可以通过在 DependencyProperty 的定义中提供默认值或在类构造函数中设置默认值来实现此目的。
当您注册依赖属性时,您可以指定一个提供默认值的 PropertyMetadata 对象。
例如,查看 DependencyProperty.Register 方法。
You could do this by either supplying a default value in the definition of the DependencyProperty or you could set the default value in your classes constructor.
When you register a dependency property you can specify a PropertyMetadata object that gives the default value.
Look at the DependencyProperty.Register method for instance.