无法数据绑定DependencyProperty [英] Cannot databind DependencyProperty

查看:142
本文介绍了无法数据绑定DependencyProperty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有DependencyProperty的UserControl。我使用数据绑定表达式在主机窗口中设置它的值。但是,它不能按预期的方式工作。



用户控件的代码段代码片段:

  public class ViewBase:UserControl 
{
public static readonly DependencyProperty ViewModelProperty
= DependencyProperty.Register(
ViewModel,typeof(ViewModelBase),typeof(ViewBase ));

public ViewModelBase ViewModel
{
get {return GetValue(ViewModelProperty)as ViewModelBase; }
set
{
SetValue(ViewModelProperty,value);
}
}
}

从XAML :CasingListView继承自ViewBase):

 < CasingEditor:CasingListView x:Name =_ casingListView
ViewModel = {Binding CasingListViewModel}/>

什么都没有。具体来说,setter从不被调用,属性保持为null。我知道源属性 CasingListViewModel 有一个值,因为我试图将它绑定到另一个属性(DataContext),并且工作正常。



我认为依赖属性可以是数据绑定。我错了吗?

解决方案

有时候会发生这个问题,这个问题并不完全是我们的想法。 b
$ b

我提到安装者从来没有打过电话。那是真实的。上面的代码略微修剪,使其更清晰。不幸的是,在调用SetValue之后,我还修剪了setter中的一个语句。该声明将值分配给DataContext,如下所示:

  public ViewModelBase ViewModel 
{
get {将GetValue(ViewModelProperty)返回为ViewModelBase; }
set
{
SetValue(ViewModelProperty,value);
DataContext = value;
}
}

正如我现在从这个优秀的文章 setter其实是绕过了<强>当通过数据绑定设置属性时。该框架反而直接针对DependencyObject。所以这个属性实际上是设置好的,但是setter从来没有被调用过(就像我所说的那样),结果是DataContext保持为null,没有任何工作。



道歉道歉,提出一个无法回答的问题。第二,作为弥补的一种方法,我可以传递非常重要的建议:


切勿将任何东西放在GetValue )和SetValue()在属性getter / setter内,因为它们并不总是被调用!


编辑:后来我也发现了这种方法的另一个问题。通过以这种方式设置DataContext,我实际上丢失了支持绑定的原始数据上下文。结果是该属性立即重置为null。总而言之,所有这一切都不是一个好办法。


I have a UserControl with a DependencyProperty. I set it's value in the host window using a data binding expression. However, it doesn't work as expected.

Snippet from the user control's codebehind:

public class ViewBase : UserControl
{
    public static readonly DependencyProperty ViewModelProperty
        = DependencyProperty.Register(
            "ViewModel", typeof(ViewModelBase), typeof(ViewBase));

    public ViewModelBase ViewModel
    {
        get { return GetValue(ViewModelProperty) as ViewModelBase; }
        set
        {
            SetValue(ViewModelProperty, value);
        }
    }
}

And from the XAML (note: CasingListView inherits from ViewBase):

<CasingEditor:CasingListView x:Name="_casingListView"
                             ViewModel="{Binding CasingListViewModel}" />

What happens is nothing. Specifically, the setter is never called, and the property remains null. I know the source property CasingListViewModel has a value, because I have tried to bind it to another property (DataContext), and it worked fine.

I thought a dependency property could be databound. Am I wrong?

解决方案

As sometimes happens, the problem turned out to be not quite what we thought.

I mentioned that the setter was never called. That is true. The code above is slightly trimmed to make it clearer. Unfortunately, I also trimmed away a statement in the setter, following the call to SetValue. That statement assigned the value to DataContext, something like this:

public ViewModelBase ViewModel
{
    get { return GetValue(ViewModelProperty) as ViewModelBase; }
    set
    {
        SetValue(ViewModelProperty, value);
        DataContext = value;
    }
}

As I have now learned from this excellent article, the setter is in fact bypassed when the property is set via databinding. The framework instead works directly against the DependencyObject. So the property was actually set, but the setter was never called (as I mentioned), and the consequence was that DataContext remained null and nothing worked.

So: First I apologize profusely for asking an unanswerable question. Second, as a way of making up for it, I can pass on the very important piece of advice:

Never put anything but GetValue() and SetValue() inside the property getter/setter, because they are not always called!

Edit: Later, I've also discovered another problem with this approach. By setting the DataContext this way, I actually lose the original data context that supports the binding in the first place. The result is that the property is immediately reset to null. So all in all not a good approach overall.

这篇关于无法数据绑定DependencyProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆