4

考虑到您在 WPF 中有一个 MVVM 架构,例如Josh Smith 的示例

您将如何实现两个相互更新的“同步”属性?我的模型中有一个 Price 属性和一个 PriceVatInclusive 属性。

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

- 反之亦然,当 PriceVatInclusive 发生变化时,我希望价格为“PriceVatInclusive / 1.21”

有什么想法吗?

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

4

2 回答 2

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 派生,则可以使用 Dependency Properties。

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
}
于 2008-12-16T20:58:32.003 回答
0

看看Polymod.NET。如果您在域对象上有“Price”属性,则可以为该域类创建模型,定义公式“PriceVat”= Price * 0.1。该模型将知道 PriceVat 在 Price 变化时发生变化,并将其告知 UI。

您的域类甚至不必是 INotifyable!

于 2012-04-26T05:39:17.943 回答