WPF:UI未更新绑定对象中的值更改

时间:2014-06-11 18:08:41

标签: c# wpf binding inotifypropertychanged

我对WPF比较新,并且遇到数据绑定问题。我将用户控件的依赖属性绑定到我的代码后面的类属性。在我的代码中实现类实体的过程中,UI通过INotifyPropertyChanged成功更新。然而,当随后更改OnPropertyChangedEventHandler后面的代码中的值时,但OnPropertyChanged方法不再回答这个问题。详情如下。如果有人能给我一些暗示我做错了,那就太棒了。

我实现了一个用户控件,我绑定到代码后面的部分类的属性CurrentAccProp.DiscountRate:

<local:doubleUEdit x:Name="InterestRate" LabelField="Interest rate" MinimumValue="0" MaximumValue="1" FormatStringForNumbers="P2" IncrementSize="0.01" UncertainValue="{Binding ElementName=RibbonWindow, Path=CurrentAccProp.DiscountRate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

CurrentAccProp是一个实例的类实现INotifyPropertyChanged以通知UI有关值的更改

//Event to inform data grid about changes
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

在DiscountRate属性的setter中调用OnPropertyChanged:

doubleU discountingrate;
    public doubleU DiscountRate 
    { 
        get {return discountingrate;} 
        set 
        {
            discountingrate = value; 
            OnPropertyChanged("DiscountingRate");
        } 
    }

我绑定到的用户控件的属性是作为依赖项属性实现的:

    //Property for data binding to doubleU
    [Description("The formatstring for the double boxes"), Category("Default")]
    public doubleU UncertainValue
    {
        get { return new doubleU(0, 0, (double)doubleUSupremum.Value, (double)doubleUSupremum.Value); }
        set { doubleURangeSlider.LowerValue = value.Interval.Infimum; doubleURangeSlider.HigherValue = value.Interval.Supremum; doubleUInfimum.Value = value.Interval.Infimum; doubleUSupremum.Value = value.Interval.Supremum; }
    }

    public static readonly DependencyProperty UncertainValueProperty =
    DependencyProperty.Register(
        "UncertainValue",
        typeof(doubleU),
        typeof(doubleUEdit),
        new PropertyMetadata(default(doubleU), OnItemsPropertyChanged));

    private static void OnItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        doubleUEdit MydblUEdt = d as doubleUEdit;

        MydblUEdt.UncertainValue = e.NewValue as doubleU;
    }

当我在OnPropertyChanged后面的代码中实例化CurrentAccProp时,会通知UI并更新值。

AccountingProperties currentaccprop = new AccountingProperties(new doubleU(0.0));
public AccountingProperties CurrentAccProp { get { return currentaccprop; } set { currentaccprop = value; } }

然而,当我稍后更新DiscountRate的值

CurrentAccProp.DiscountRate = new doubleU(1.0);

OnPropertyChanged被执行,但UI不再更新。有没有人知道我在这里做错了什么?

HighCore和zaknotzach指出的错字确实是问题所在。谢谢你的帮助!我在HighCore引用的线程中实现了这种方法,以避免这种情况,它就像一个魅力。在更改的AccountingProperties类下面,实例化CurrentAccProp以供参考:

public class AccountingProperties : INotifyPropertyChanged
{
    doubleU discountrate;
    public doubleU DiscountRate 
    { 
        get {return discountrate;} 
        set { SetField(ref discountrate, value, () => DiscountRate); }
    }

    //------------------------------------------------
    //constructors
    public AccountingProperties(doubleU discountrate)
    {
        DiscountRate = discountrate;
    }

    //Event to inform data grid about changes
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }


    protected virtual void OnPropertyChanged<T>(Expression<Func<T>> selectorExpression)
    {
        if (selectorExpression == null)
            throw new ArgumentNullException("selectorExpression");
        MemberExpression body = selectorExpression.Body as MemberExpression;
        if (body == null)
            throw new ArgumentException("The body must be a member expression");
        OnPropertyChanged(body.Member.Name);
    }

    protected bool SetField<T>(ref T field, T value, Expression<Func<T>> selectorExpression)
    {
        if (EqualityComparer<T>.Default.Equals(field, value)) return false;
        field = value;
        OnPropertyChanged(selectorExpression);
        return true;
    }


}

2 个答案:

答案 0 :(得分:5)

您需要先在

中更改字符串
OnPropertyChanged("DiscountingRate");

到&#34; DiscountRate&#34;。您为OnPropertyChanged函数提供的字符串必须与属性名称匹配。这很可能是你遇到的问题。

答案 1 :(得分:5)

正如已经回答的那样,问题是OnPropertyChanged("DiscountingRate");为事件提供了错误的属性名称。

为了防止这样的错误,您可以避免一起使用字符串文字。在OnPropertyChanged参数中,使用CallerMemberName。您可以将OnPropertyChanged签名修改为

public void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
    // Do your stuff
}

然后在你的二传手中,你只需拨打this.OnPropertyChanged();。该方法将被赋予已更改的属性名称。

public doubleU DiscountRate 
{ 
    get {return discountingrate;} 
    set 
    {
        discountingrate = value; 
        OnPropertyChanged();
    } 
}

这样做的好处是,您可以重构代码,而不必担心破坏您的属性更改事件。