从另一个视图模型中侦听一个视图模型上的依赖项属性更改

时间:2014-05-28 17:16:09

标签: c# wpf mvvm

我的WPF应用程序中有一个数据输入视图的视图模型。使用MVVM,我试图考虑一个场景,在这种情况下,如果需要,可以“运送给客户”和不同的“向客户收费”。

我的观点有两个部分:结算客户和发货客户。发货客户可以从结算客户自动填写,或者根本不填写(如果没有发货)或手动填写(如果运费不同)。

View shot

该帐单目前通过XAML绑定到我的MainWindowViewModel.BilledCustomer的属性。 “BeingShipped”复选框调节stackpanel,它通过IsChecked属性上的数据绑定保存Ship to的所有字段。

 public class MainWindowViewModel : ViewViewModelBase
{
    public bool Validated = false;
    public bool Saved = false;

    private MenuViewModel _menumodel;

    public ObservableCollection<BillablePartViewModel> WorkOrderParts { get; set; }

    public ObservableCollection<BillableServiceViewModel> WorkOrderServices { get; set; }

    public CustomerViewModel BilledCustomer { get; set; }

    public CustomerViewModel ShippedCustomer { get; set; }

    public WorkOrderViewModel WorkOrder { get; set; }

    //App.config fields
    public string Company_Name { get; set; }
    public string Company_Address { get; set; }
    public string Company_Phone { get; set; }
    public string Company_Fax { get; set; }
    public string Company_Site { get; set; }
    public string Company_Department { get; set; }

    public MainWindowViewModel(ObservableCollection<BillablePartViewModel> parts, ObservableCollection<BillableServiceViewModel> services
        , CustomerViewModel billedCustomer, CustomerViewModel shippedCustomer, WorkOrderViewModel workOrder
        , CommandBindingCollection bindings) : base(bindings)
    {
        this.WorkOrderParts = parts;
        this.WorkOrderServices = services;
        this.BilledCustomer = billedCustomer;
        this.ShippedCustomer = shippedCustomer;
        this.WorkOrder = workOrder;
        GetCompanyInfo();
    }
    public MainWindowViewModel(CommandBindingCollection bindings) : base(bindings)
    {
        _menumodel = new MenuViewModel(bindings);
        this.WorkOrder = new WorkOrderViewModel();
        GetCompanyInfo();
    }  

客户视图模型###

 public class CustomerViewModel : Accu_Base_Lib.Bases.ModelViewModelBase<DAL.Customer>
{



    public string Name
    {
        get { return (string)GetValue(NameProperty); }
        set { SetValue(NameProperty, value);
        string[] names = value.Split(' ');
            if (names.Length > 1)
            {
                this.FirstName = names[0];
                this.LastName = names[1];
            }

            else if (names.Length == 1)
            {
                this.FirstName = names[0];
            }
            else
            {
                //name deleted
                this.FirstName = string.Empty;
                this.LastName = string.Empty;
            }
        }
    }

    // Using a DependencyProperty as the backing store for Name.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty NameProperty =
        DependencyProperty.Register("Name", typeof(string), typeof(CustomerViewModel));




    public string Id
    {
        get { return (string)GetValue(IdProperty); }
        set { SetValue(IdProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Id.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty IdProperty =
        DependencyProperty.Register("Id", typeof(string), typeof(CustomerViewModel));




    public string FirstName
    {
        get { return (string)GetValue(FirstNameProperty); }
        set { SetValue(FirstNameProperty, value);
        this.Name = this.FirstName + " " + this.LastName;
        }
    }

    // Using a DependencyProperty as the backing store for FirstName.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty FirstNameProperty =
        DependencyProperty.Register("FirstName", typeof(string), typeof(CustomerViewModel));




    public string CompanyName
    {
        get { return (string)GetValue(CompanyNameProperty); }
        set { SetValue(CompanyNameProperty, value); }
    }

    // Using a DependencyProperty as the backing store for CompanyName.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CompanyNameProperty =
        DependencyProperty.Register("CompanyName", typeof(string), typeof(CustomerViewModel));





    public string StreetAddress
    {
        get { return (string)GetValue(StreetAddressProperty); }
        set { SetValue(StreetAddressProperty, value); }
    }

    // Using a DependencyProperty as the backing store for StreetAddress.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty StreetAddressProperty =
        DependencyProperty.Register("StreetAddress", typeof(string), typeof(CustomerViewModel));







    public string City
    {
        get { return (string)GetValue(CityProperty); }
        set { SetValue(CityProperty, value); }
    }

    // Using a DependencyProperty as the backing store for City.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CityProperty =
        DependencyProperty.Register("City", typeof(string), typeof(CustomerViewModel));





    public string State
    {
        get { return (string)GetValue(StateProperty); }
        set { SetValue(StateProperty, value); }
    }

    // Using a DependencyProperty as the backing store for State.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty StateProperty =
        DependencyProperty.Register("State", typeof(string), typeof(CustomerViewModel));




    public string Zip
    {
        get { return (string)GetValue(ZipProperty); }
        set { SetValue(ZipProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Zip.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ZipProperty =
        DependencyProperty.Register("Zip", typeof(string), typeof(CustomerViewModel));




    public string Phone
    {
        get { return (string)GetValue(PhoneProperty); }
        set { SetValue(PhoneProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Phone.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty PhoneProperty =
        DependencyProperty.Register("Phone", typeof(string), typeof(CustomerViewModel));




    public string LastName
    {
        get { return (string)GetValue(LastNameProperty); }
        set { SetValue(LastNameProperty, value);
        this.Name = this.FirstName + " " + this.LastName;
        }
    }

    // Using a DependencyProperty as the backing store for LastName.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty LastNameProperty =
        DependencyProperty.Register("LastName", typeof(string), typeof(CustomerViewModel));




    public string Email
    {
        get { return (string)GetValue(EmailProperty); }
        set { SetValue(EmailProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Email.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty EmailProperty =
        DependencyProperty.Register("Email", typeof(string), typeof(CustomerViewModel));













    public CustomerViewModel(DAL.Customer model)

    {
        base.Init(model);
        //Fills this viewmodel based off the model supplied.
        UpdateViewModelFromModel();
    }

    public CustomerViewModel() 
    {
        base.Init();
        this.Model.Id = Guid.NewGuid().ToString();
        UpdateViewModelFromModel();
    }  

我考虑过在实际的CustomerViewModel中为这个场景设置条件,但是如果它的逻辑只在一个视图中使用,我不想为类添加逻辑。我希望将逻辑保持在我的实际MainWindowViewModel范围内。如何使用MVVM方法实现这一目标?

1 个答案:

答案 0 :(得分:0)

对于您的班级 CustomerViewModel ,我只能在其中看到字符串类型的字段,因此您可以使用MemberwiseClone()方法为CustomerViewModel创建克隆对象。

public class CustomerViewModel
{
   .....

   public CutomerViewModel Clone()
   {
      return (CustomerViewModel)MemberwiseClone();
   }

   .....
}

在发货时 MainWindowViewModel 中选中复选框:

ShippedCustomer = BilledCustomer.Clone();