[英]Dependency Property Call in code behind
I have created a custom UserControl with some Dependency properties. 我创建了一个具有某些Dependency属性的自定义UserControl。
This custom control is hosted on a Window. 此自定义控件托管在Window上。
When I try to get a value from a DependecyProperty in code behind it doesn't work. 当我尝试从DependecyProperty中的代码中获取值时,它不起作用。
public static readonly DependencyProperty ValueDp = DependencyProperty.Register("Value", typeof(string), typeof(MyCustomUserControl), new FrameworkPropertyMetadata(string.Empty, OutputHandler));
public string Value
{
get { return (string)GetValue(ValueDp); }
set { SetValue(ValueDp, value); }
}
private static void OutputHandler(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var temp= dependencyObject as MyCustomUserControl;
if (temp!= null)
{
dependencyObject.SetValue(ValueDp,temp._conversionValue);
}
}
On the host I have put a button and when I click on it, I want to read the value stored in the DP, but I will always get the default value set in DP. 在主机上,我已经放置了一个按钮,当我单击它时,我想读取DP中存储的值,但是我将始终获得DP中设置的默认值。
Any ideas what I`m doing wrong here? 有什么想法我在这里做错了吗?
Regards 问候
我认为在OutputHandler
方法中,您总是会丢弃分配给该属性的新值( dependencyPropertyChangedEventArgs.NewValue
)
As @Alberto has said the OldValue
and NewValue
are the properties which hold the value of the DependencyProperty
. 正如@Alberto所说的,
OldValue
和NewValue
是保存DependencyProperty
值的属性。 The above properties are found in dependencyPropertyChangedEventArgs
. 以上属性可以在
dependencyPropertyChangedEventArgs
中找到。 In your Handler
the member dependencyObject
and temp
refer to the same object. 在您的
Handler
,成员dependencyObject
和temp
引用相同的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.