WPF MVVM中的两个“自我更新”属性

时间:2008-12-16 20:48:56

标签: wpf architecture mvvm

考虑到你在WPF中有一个MVVM架构,如Josh Smith's examples

你如何实现两个同步更新的属性'同步'?我的模型中有Price属性和PriceVatInclusive属性。

- 当价格发生变化时,我希望看到增值税的价格自动为“价格* 1.21”。

- 反之亦然,当PriceVatInclusive改变时,我希望价格为'PriceVatInclusive / 1.21'

有关于此的任何想法吗?

如果您的模型是实体框架,那该怎么办?你不能使用上面的方法......不是吗?你应该把计算代码放在ViewMOdel中还是......?

2 个答案:

答案 0 :(得分:4)

一种方式:

    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
}

答案 1 :(得分:0)

看看Polymod.NET。 如果域对象上有“价格”属性,则可以为该域类创建模型,定义公式“PriceVat”= Price * 0.1。该模型将知道价格变化时PriceVat会发生变化,并告诉用户界面。

您的域类甚至不必是INotifyable!