English 中文(简体)
WPF MVVM中的两个自更新属性
原标题:
  • 时间:2008-12-16 20:48:56
  •  标签:

考虑到您在 WPF 中使用了 MVVM 架构,比如 Josh Smith 的示例

你会如何实现两个同步的属性,它们会相互更新?我在我的模型中有一个Price属性和一个PriceVatInclusive属性。

当价格变化时,我希望自动看到含增值税的价格为Price * 1.21。

反之亦然,当PriceVatInclusive改变时,我希望Price为PriceVatInclusive / 1.21

那方面有什么想法吗?

如果您的模型是实体框架实体,那该怎么办?那您就不能使用上述方法,对吗?您应该将计算代码放在ViewModel中还是其他地方?

最佳回答

一种方法:

    public class Sample : INotifyPropertyChanged
    {
        private const double Multiplier = 1.21;
        #region Fields
        private double price;
        private double vat;
        #endregion

        #region Properties
        public double Price
        {
            get { return price; }
            set
            {
                if (price == value) return;
                price = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Price"));
                Vat = Price * Multiplier;
            }
        }

        public double Vat
        {
            get { return vat; }
            set
            {
                if (vat == value) return;
                vat = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Vat"));
                Price = Vat / Multiplier;
            }
        }
        #endregion

        #region INotifyPropertyChanged Members
        protected void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler ev = PropertyChanged;
            if (ev != null)
            {
                ev(this, e);
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }

如果您可以从DependencyObject派生,则可以使用依赖属性。

public class Sample : DependencyObject
{
    private const double Multiplier = 1.21;

    public static readonly DependencyProperty VatProperty =
        DependencyProperty.Register("Vat", typeof(double), typeof(Sample), 
        new PropertyMetadata(VatPropertyChanged));

    public static readonly DependencyProperty PriceProperty =
        DependencyProperty.Register("Price", typeof(double), typeof(Sample), 
        new PropertyMetadata(PricePropertyChanged));

    private static void VatPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        Sample sample = obj as Sample;
        sample.Price = sample.Vat / Multiplier;
    }

    private static void PricePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        Sample sample = obj as Sample;
        sample.Vat = sample.Price * Multiplier;
    }

    #region Properties
    public double Price
    {
        get { return (double)GetValue(PriceProperty); }
        set { SetValue(PriceProperty, value); }
    }

    public double Vat
    {
        get { return (double)GetValue(VatProperty); }
        set { SetValue(VatProperty, value); }
    }
    #endregion
}
问题回答

Have a look at Polymod.NET. If you have a Price property on a domain object, you can create a model for that domain class, define a formula PriceVat = Price * 0.1. The model will know that PriceVat changes when Price changes, and tell the UI about it.

你的域类甚至不必是INotifyable!





相关问题
热门标签