简体   繁体   English

在代码后面的依赖属性调用

[英]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所说的, OldValueNewValue是保存DependencyProperty值的属性。 The above properties are found in dependencyPropertyChangedEventArgs . 以上属性可以在dependencyPropertyChangedEventArgs中找到。 In your Handler the member dependencyObject and temp refer to the same object. 在您的Handler ,成员dependencyObjecttemp引用相同的对象。

暂无
暂无

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

相关问题 在后面的代码中将依赖属性绑定到另一个依赖属性 - bind Dependency Property to Another Dependency Property in Code Behind 如何设置在代码隐藏中声明的依赖属性 - How to set Dependency Property declared in code-behind 在将(视图的)背后代码中的依赖属性绑定到其视图模型的属性时,我在做错什么? - What am I doing wrong in this binding of a dependency property in the code behind (of a view) to a property its viewmodel? 在后面的代码中使属性动画化 - Animate property in code behind 如何从 xaml 中的 ItemTemplate 访问在代码中定义的用户控件的依赖项属性? - How can I access a dependency property of a user control that was defined in code behind from a ItemTemplate in xaml? UserControl中的TextBox,无法从后面的代码中编辑Text依赖项属性 - TextBox in UserControl, can't edit Text dependency property from code behind 窗口级依赖项属性(视图的代码位于后面),可以将其绑定到视图的XAML中吗? - Window level dependency property (view's code behind), can it be bound to in XAML of the view? 为什么我需要绑定到后面的代码中的依赖项属性,而不是在视图模型中? - Why do I need to bind to a dependency property in code behind but not in view model? 在Javascript中抓取属性背后的代码? - Grab code behind property in Javascript? 在后面的代码中绑定静态属性 - Binding static property in code behind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM