簡體   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