4

Josh Smithの例のようなWPFにMVVMアーキテクチャがあることを考えると

互いに更新する 2 つのプロパティを「同期」して実装するにはどうすればよいでしょうか? モデルに Price プロパティと PriceVatInclusive プロパティがあります。

-価格が変更されたときに、付加価値税込みの価格が自動的に「価格 * 1.21」になるようにしたい。

-逆に、PriceVatInclusive が変更された場合、Price を「PriceVatInclusive / 1.21」にしたい

それに関するアイデアはありますか?

また、モデルが Entity フレームワーク全体である場合はどうなるでしょうか? 上記のアプローチを使用することはできません...いいえ?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を見てください。ドメイン オブジェクトに「価格」プロパティがある場合、そのドメイン クラスのモデルを作成し、式「PriceVat」= 価格 * 0.1 を定義できます。モデルは、Price が変更されると PriceVat が変更されることを認識し、それについて UI に伝えます。

ドメイン クラスは INotifyable である必要さえありません。

于 2012-04-26T05:39:17.943 に答える