繁体   English   中英

侦听一个视图模型与另一个视图模型之间的依赖项属性更改

[英]Listening to dependency property changes on one viewmodel from another viewmodel

我在WPF应用程序中有一个数据输入视图的视图模型。 使用MVVM,我试图说明一种情况,在这种情况下,有可能需要“交付给客户”,而有可能需要另外的“支付给客户”。

我的观点分为两部分:开票客户和运输客户。 发货客户可以从开票客户处自动填写,也可以完全不填写(如果不发货),也可以手动填写(如果送货对象不同)。

查看镜头

当前的帐单已通过XAML绑定到我的MainWindowViewModel.BilledCustomer的属性。 “ BeingShipped”复选框正在调节堆栈面板,该堆栈面板通过IsChecked属性上的数据绑定保存Ship的所有字段。

 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为此场景在Setter中设置条件,但我不想将逻辑添加到类中,前提是该逻辑仅在一个视图中使用。 我想将逻辑保留在实际的MainWindowViewModel 如何使用MVVM方法完成此任务?

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

public class CustomerViewModel
{
   .....

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

   .....
}

并在您的MainWindowViewModel ,“被运送”复选框被选中:

ShippedCustomer = BilledCustomer.Clone();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM