2017-02-08 1 views
-1

Ich habe zwei UserControls Payroll und TabLayout. Die Abrechnung enthält eine ToolBar und TabControl und TabLayout enthält ein Grid. Die TabItems für TabControl in der Personalabrechnung werden mit TabLayout über ObservableCollection in meinem MainViewModel gefüllt.wpf - Duplizieren von Informationen verhindern MVVM

Vor etwa einer Stunde habe ich diese Frage gestellt: WPF Unable to retrieve binding values MVVM, und habe es gelöst.

Jetzt bin ich mit einem Problem konfrontiert, wo, weil das ViewModel jetzt eine einzelne Instanz ist, die Informationen in einem Tab zu einem anderen übertragen werden. Daher zeigen alle Tabs dasselbe.

Wenn ich eine neue Instanz des ViewModel für jede Registerkarte erstellen, kann ich die Bindungswerte nicht mehr abrufen.

MainViewModel:

namespace OcelotPayroll.ViewModel 
{ 
    public class MainViewModel : ViewModelBase 
    {  
     public ObservableCollection<WorkspaceViewModel> Workspaces { get; set; }    

     public MainViewModel() 
     { 
      Workspaces = new ObservableCollection<WorkspaceViewModel>(); 
     } 


     private DelegateCommand _newWorkspaceCommand; 
     public ICommand NewWorkspaceCommand 
     { 
      get { return _newWorkspaceCommand ?? (_newWorkspaceCommand = new DelegateCommand(NewWorkspace)); } 
     } 

     private void NewWorkspace() 
     { 

      var workspace = new WorkspaceViewModel 
      { 
       LoadedControl = new TabLayout() { DataContext = this } 
      }; 
      Workspaces.Add(workspace); 
      SelectedIndex = Workspaces.IndexOf(workspace); 
     } 
    } 
} 

Abrechnungsdatacontext gesetzt:

public Payroll() 
    { 
     InitializeComponent(); 
     DataContext = new PayslipModel(); 
    } 

PayslipModel/VM:

namespace OcelotPayroll 
{ 
    public class PayslipModel : EmployeeModel 
    {  
     private decimal _amount; 

     public decimal Amount 
     { 
      get 
      { 
       return _amount; 
      } 
      set 
      { 
       _amount = value; 
       OnPropertyChanged("Amount"); 
      } 
     } 

     public string NisName 
     { 
      get 
      { 
       return _nis; 
      } 
      set 
      { 
       _nis = value; 
       OnPropertyChanged("NisName"); 
      } 
     } 

     public string EdTaxName 
     { 
      get 
      { 
       return _edtax; 
      } 
      set 
      { 
       _edtax = value; 
       OnPropertyChanged(); 
      } 
     } 

     public string NhtName 
     { 
      get 
      { 
       return _nht; 
      } 
      set 
      { 
       _nht = value; 
       OnPropertyChanged(); 
      } 
     } 

     public string PayeName 
     { 
      get 
      { 
       return _paye; 
      } 
      set 
      { 
       _paye = value; 
       OnPropertyChanged(); 
      } 
     } 

     public string NisVal 
     { 
      get 
      { 
       return _nisVal; 
      } 
      set 
      { 
       _nisVal = value; 
       OnPropertyChanged(); 
      } 
     } 

     public string EdTaxVal 
     { 
      get 
      { 
       return _edVal; 
      } 
      set 
      { 
       _edVal = value; 
       OnPropertyChanged(); 
      } 
     } 

     public string NhtVal 
     { 
      get 
      { 
       return _nhtVal; 
      } 
      set 
      { 
       _nhtVal = value; 
       OnPropertyChanged(); 
      } 
     } 

     public string PayeVal 
     { 
      get 
      { 
       return _payeVal; 
      } 
      set 
      { 
       _payeVal = value; 
       OnPropertyChanged(); 
      } 
     } 

     public decimal StautoryIncome 
     { 
      get 
      { 
       return statIncome; 
      } 
      set 
      { 
       statIncome = value; 
      } 
     } 

     public string TotalDeduction 
     { 
      get 
      { 
       return totalDed; 
      } 
      set 
      { 
       totalDed = value; 
       OnPropertyChanged(); 
      } 
     } 

     public string NetPay 
     { 
      get 
      { 
       return _netpay; 
      } 
      set 
      { 
       _netpay = value; 
       OnPropertyChanged(); 
      } 
     } 

     public bool NisChecked 
     { 
      get 
      { 
       return nisIsChecked; 
      } 
      set 
      { 
       nisIsChecked = value; 

       OnPropertyChanged("NisChecked"); 

       CalcNis(); 
      } 
     } 

     public bool EdTaxChecked 
     { 
      get 
      { 
       return edtaxIsChecked; 
      } 
      set 
      { 
       edtaxIsChecked = value; 
       OnPropertyChanged(); 
       CalcEdTax(); 
      } 
     } 

     public bool NhtChecked 
     { 
      get 
      { 
       return nhtIsChecked; 
      } 
      set 
      { 
       nhtIsChecked = value; 
       OnPropertyChanged(); 
       CalcNht(); 
      } 
     } 

     public bool PayeChecked 
     { 
      get 
      { 
       return payeIsChecked; 
      } 
      set 
      { 
       payeIsChecked = value; 
       OnPropertyChanged(); 
       CalcPaye(); 
      } 
     } 

     public bool LevyChecked 
     { 
      get 
      { 
       return levyIsChecked; 
      } 
      set 
      { 
       levyIsChecked = value; 
       OnPropertyChanged(); 
       CalcLevy(); 
      } 
     } 

     public bool WeeklyChecked 
     { 
      get 
      { 
       return wklyIsChecked; 
      } 
      set 
      { 
       wklyIsChecked = value; 
       OnPropertyChanged(); 
      } 
     } 

     public bool FnChecked 
     { 
      get 
      { 
       return fnIsChecked; 
      } 
      set 
      { 
       if (fnIsChecked == value) 
        return; 

       fnIsChecked = value; 
       OnPropertyChanged(); 
      } 
     } 
     public bool MonthlyChecked 
     { 
      get 
      { 
       return monthlyIsChecked; 
      } 
      set 
      { 
       if (monthlyIsChecked == value) 
        return; 

       monthlyIsChecked = value; 
       OnPropertyChanged(); 
      } 
     } 

     private DateTime _date; 
     public DateTime Date 
     { 
      get 
      { 
       return _date; 

      } 
      set 
      { 
       _date = value; 
      } 
     } 

     private string _annualGross; 

     public string AnnualGross 
     { 
      get 
      { 
       return _annualGross; 
      } 
      set 
      { 
       _annualGross = value; 
       OnPropertyChanged(); 
      } 
     } 

     private string _annualNis; 

     public string AnnualNis 
     { 
      get 
      { 
       return _annualNis; 
      } 
      set 
      { 
       _annualNis = value; 
       OnPropertyChanged(); 
      } 
     } 

     private string _annualNht; 

     public string AnnualNht 
     { 
      get 
      { 
       return _annualNht; 
      } 
      set 
      { 
       _annualNht = value; 
       OnPropertyChanged(); 
      } 
     } 

     private string _annualEdtax; 

     public string AnnualEdTax 
     { 
      get 
      { 
       return _annualEdtax; 
      } 
      set 
      { 
       _annualEdtax = value; 
       OnPropertyChanged(); 
      } 
     } 

     private string _annualPaye; 

     public string AnnualPaye 
     { 
      get 
      { 
       return _annualPaye; 
      } 
      set 
      { 
       _annualPaye = value; 
       OnPropertyChanged(); 
      } 
     } 

     public PayslipModel() 
     {  
      Date = DateTime.Today.Date; 

      SelectAll = new DelegateCommand(Select,() => CanSelect); 

      UnSelectAll = new DelegateCommand(UnSelect,() => CanUnSelect); 

      SaveToDatabase = new DelegateCommand(Save,() => CanSave); 

      scon = new MichaelAllenEntities(); 

     } 

     public ICommand SelectAll 
     { 
      get; set; 
     } 

     private bool CanSelect 
     { 
      get { return Hide1 = true; } 
     } 

     private void Select() 
     { 
      if (Hide1 == true) 
      { 
       NisChecked = true; 
       EdTaxChecked = true; 
       NhtChecked = true; 
       PayeChecked = true; 
      } 
     } 

     public ICommand UnSelectAll 
     { 
      get; set; 
     } 

     private bool CanUnSelect 
     { 
      get { return Hide1 = true; } 
     } 

     private void UnSelect() 
     { 
      if (Hide1 == true) 
      { 
       NisChecked = false; 
       EdTaxChecked = false; 
       NhtChecked = false; 
       PayeChecked = false; 
      } 
     } 

     public ICommand SaveToDatabase 
     { 
      get; set; 
     } 

     private bool CanSave 
     { 
      get { return Workspaces.Count > 0; } 
     }  

     private async void Save() 
     { 
      try 
      { 
       var salary = new SalaryInfo 
       { 
        EmpId = SelectedEmployee.EmpId, 
        EmpName = SelectedEmployee.EmpName, 
        NIS = decimal.Parse(NisVal.ToString()), 
        NHT = decimal.Parse(NhtVal.ToString()), 
        EdTax = decimal.Parse(EdTaxVal.ToString()), 
        PAYE = decimal.Parse(PayeVal.ToString()), 
        TotalTax = decimal.Parse(TotalDeduction.ToString()), 
        StatutoryIncome = StautoryIncome, 
        GrossPay = Amount, 
        NetPay = decimal.Parse(NetPay.ToString()), 
        Date = Date, 
        Earnings = Earnings, 
        PayPeriod = SelectedValueOne.Value 
       }; 
       scon.SalaryInfoes.Add(salary); 
       scon.SaveChanges(); 
      } 
      catch (DbEntityValidationException ex) 
      { 

      } 
      catch(Exception ex) 
      { 
       var exceptionDialog = new MessageDialog 
       { 
        Message = { Text = string.Format("{0}", ex) } 
       }; 

       await DialogHost.Show(exceptionDialog, "RootDialog"); 
      } 

     } 

     private bool _hide1 = true; 

     public bool Hide1 
     { 
      get 
      { 
       return _hide1; 
      } 
      set 
      { 
       _hide1 = value; 
       OnPropertyChanged(); 
      } 
     } 

     private bool _hide2 = false; 

     public bool Hide2 
     { 
      get 
      { 
       return _hide2; 
      } 
      set 
      { 
       _hide2 = value; 
       OnPropertyChanged(); 
      } 
     } 

     private EmployeeInfo _selectedEmployee; 

     public new EmployeeInfo SelectedEmployee 
     { 
      get { return _selectedEmployee; } 
      set 
      { 
       _selectedEmployee = value; 
       OnPropertyChanged(); 

       if(SelectedEmployee.EmployedUnder == "Michael Allen") 
       { 
        Hide1 = true; 
        Hide2 = false; 
       } 
       if(SelectedEmployee.EmployedUnder == "Barachel Ltd") 
       { 
        Hide2 = true; 
        Hide1 = false; 
       } 
      } 
     } 

     private string _hours; 

     public string Hours 
     { 
      get 
      { 
       return _hours; 
      } 
      set 
      { 
       _hours = value; 
       OnPropertyChanged(); 
      } 
     } 

     private string _rate; 

     public string Rate 
     { 
      get 
      { 
       return _rate; 
      } 
      set 
      { 
       _rate = value; 
       OnPropertyChanged(); 
       if (Rate != string.Empty || Hours != string.Empty) 
       { 
        Amount = decimal.Parse((double.Parse(Hours.ToString()) * double.Parse(Rate.ToString())).ToString()); 
       }else 
       { 
        Amount = 0; 
       } 
      } 
     } 

     private string _selectedDate; 

     public string SelectedDate 
     { 
      get 
      { 
       return _selectedDate; 
      } 
      set 
      { 
       _selectedDate = value; 
       OnPropertyChanged(); 
      } 
     } 

     private string _earnings; 

     public string Earnings 
     { 
      get 
      { 
       return _earnings; 
      } 
      set 
      { 
       _earnings = value; 
       OnPropertyChanged(); 
      } 
     } 

     #region Calculations 

     public async void CalcNis() 
     { 
      try 
      { 
       float nis = 0; 

       NisName = "N.I.S."; 

       SqlConnection con = new SqlConnection(scon.Database.Connection.ConnectionString); 
       con.Open(); 
       SqlCommand cmd = new SqlCommand("select NIS from Deductions where TaxId='1'", con); 

       SqlDataReader sdr = cmd.ExecuteReader(); 
       while (sdr.Read()) 
       { 
        nis = float.Parse(sdr.GetValue(0).ToString()); 
       } 

       con.Close(); 

       if (Amount != 0) 
       { 
        NisVal = (decimal.Parse(Amount.ToString()) * decimal.Parse(nis.ToString())).ToString("N2"); 
        StautoryIncome = (decimal.Parse(Amount.ToString()) - decimal.Parse(NisVal.ToString())); 
       } 
      } 
      catch(Exception ex) 
      { 
       var exceptionDialog = new MessageDialog 
       { 
        Message = { Text = ex.ToString() } 
       }; 

       await DialogHost.Show(exceptionDialog, "RootDialog"); 
      } 
     } 

     public async void CalcLevy() 
     { 
      try 
      { 
       if(LevyChecked == true) 
       { 
        float levy = 0; 

        NisName = "Contractor's Levy"; 

        SqlConnection con = new SqlConnection(scon.Database.Connection.ConnectionString); 
        con.Open(); 
        SqlCommand cmd = new SqlCommand("select ContractorLevy from Deductions where TaxId='1'", con); 

        SqlDataReader sdr = cmd.ExecuteReader(); 
        while (sdr.Read()) 
        { 
         levy = float.Parse(sdr.GetValue(0).ToString()); 
        } 

        con.Close(); 

        if (Amount != 0) 
        { 

         NisVal = (double.Parse(Amount.ToString()) * double.Parse(levy.ToString())).ToString("N2"); 

         TotalDeduction = double.Parse(NisVal.ToString()).ToString("N2"); 

         NetPay = (double.Parse(Amount.ToString()) - double.Parse(NisVal.ToString())).ToString("N2"); 
        } 
       }else 
        if(LevyChecked == false) 
       { 
        NisVal = string.Empty; 
        TotalDeduction = string.Empty; 
        NetPay = string.Empty; 
       } 
      } 
      catch (Exception ex) 
      { 
       var exceptionDialog = new MessageDialog 
       { 
        Message = { Text = ex.ToString() } 
       }; 

       await DialogHost.Show(exceptionDialog, "RootDialog"); 
      } 
     } 

     public async void CalcEdTax() 
     { 
      try 
      { 
       float edtax = 0; 

       EdTaxName = "EDUCATION TAX"; 

       SqlConnection con = new SqlConnection(scon.Database.Connection.ConnectionString); 
       con.Open(); 
       SqlCommand cmd = new SqlCommand("select EdTax from Deductions where TaxId='1'", con); 

       SqlDataReader sdr = cmd.ExecuteReader(); 
       while (sdr.Read()) 
       { 
        edtax = float.Parse(sdr.GetValue(0).ToString()); 
       } 

       con.Close(); 

       if (EdTaxName != string.Empty) 
       { 
        if (StautoryIncome != 0) 
        { 
         EdTaxVal = (statIncome * decimal.Parse(edtax.ToString())).ToString("N2"); 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       var exceptionDialog = new MessageDialog 
       { 
        Message = { Text = ex.ToString() } 
       }; 

       await DialogHost.Show(exceptionDialog, "RootDialog"); 
      } 
     } 

     public async void CalcNht() 
     { 
      try 
      { 
       float nht = 0; 

       NhtName = "N.H.T."; 

       SqlConnection con = new SqlConnection(scon.Database.Connection.ConnectionString); 
       con.Open(); 
       SqlCommand cmd = new SqlCommand("select NHT from Deductions where TaxId='1'", con); 

       SqlDataReader sdr = cmd.ExecuteReader(); 
       while (sdr.Read()) 
       { 
        nht = float.Parse(sdr.GetValue(0).ToString()); 
       } 

       con.Close(); 

       if (Amount != 0) 
       { 
        NhtVal = (decimal.Parse(Amount.ToString()) * decimal.Parse(nht.ToString())).ToString("N2"); 
       } 
      } 
      catch (Exception ex) 
      { 
       var exceptionDialog = new MessageDialog 
       { 
        Message = { Text = ex.ToString() } 
       }; 

       await DialogHost.Show(exceptionDialog, "RootDialog"); 
      } 
     } 

     private decimal _totalAmount; 

     public decimal TotalAmount 
     { 
      get 
      { 
       return _totalAmount; 
      } 
      set 
      { 
       _totalAmount = value; 
       OnPropertyChanged(); 
      } 
     } 

     public async void CalcPaye() 
     { 
      try 
      { 
       float paye = 0; 
       decimal threshold = 0, taxable = 0, above = 0.3M; 

       if (PayeChecked == true) 
       { 
        PayeName = "P.A.Y.E."; 

        SqlConnection con = new SqlConnection(scon.Database.Connection.ConnectionString); 
        con.Open(); 
        SqlCommand cmd = new SqlCommand("select PAYE, FNTaxThreshold from Deductions where TaxId='1'", con); 

        SqlDataReader sdr = cmd.ExecuteReader(); 

        if (Amount != 0) 
        { 
         while (sdr.Read()) 
         { 
          paye = float.Parse(sdr.GetValue(0).ToString()); 
          threshold = decimal.Parse(sdr.GetValue(1).ToString()); 
         } 

         con.Close(); 

         // 
         //Fortnightly 
         // 
         if (FnChecked == true) 
         { 

          if (threshold <= decimal.Parse(Amount.ToString())) 
          { 
           if ((double.Parse(Amount.ToString()) * 2) * 12 <= 796536) 
           { 
            taxable = StautoryIncome - threshold; 
            PayeVal = (taxable * 0).ToString("N2"); 
           } 
           else if ((double.Parse(Amount.ToString()) * 2) * 12 >= 796536 && (double.Parse(Amount.ToString()) * 2) * 12 <= 6000000) 
           { 
            taxable = StautoryIncome - threshold; 
            PayeVal = (taxable * decimal.Parse(paye.ToString())).ToString("N2"); 
           } 
           else if ((double.Parse(Amount.ToString()) * 2) * 12 > 6000000) 
           { 
            taxable = StautoryIncome - threshold; 
            PayeVal = (taxable * above).ToString("N2"); 
           } 

          } 
         } 

         // 
         //Monthly Checked 
         // 
         if (MonthlyChecked == true) 
         { 
          con.Open(); 

          cmd = new SqlCommand("select MonthlyThreshold from Deductions where TaxId='1'", con); 
          sdr = cmd.ExecuteReader(); 

          while (sdr.Read()) 
          { 
           threshold = decimal.Parse(sdr.GetValue(0).ToString()); 
          } 

          con.Close(); 

          if (threshold <= decimal.Parse(Amount.ToString())) 
          { 
           if ((double.Parse(Amount.ToString()) * 2) * 12 <= 796536) 
           { 
            taxable = StautoryIncome - threshold; 
            PayeVal = (taxable * 0).ToString("N2"); 
           } 
           else if ((double.Parse(Amount.ToString()) * 2) * 12 >= 796536 && (double.Parse(Amount.ToString()) * 2) * 12 <= 6000000) 
           { 
            taxable = StautoryIncome - threshold; 
            PayeVal = (taxable * decimal.Parse(paye.ToString())).ToString("N2"); 
           } 
           else if ((double.Parse(Amount.ToString()) * 2) * 12 > 6000000) 
           { 
            taxable = StautoryIncome - threshold; 
            PayeVal = (taxable * above).ToString("N2"); 
           } 

          } 
          else if (threshold > decimal.Parse(Amount.ToString())) 
          { 
           PayeVal = (0).ToString("N2"); 
          } 
         } 

        } 

        if (EdTaxVal != string.Empty || NhtVal != string.Empty || NisVal != string.Empty || PayeVal != string.Empty) 
        { 
         double total = 0; 


         total = double.Parse(NisVal.ToString()) + double.Parse(PayeVal.ToString()) + double.Parse(NhtVal.ToString()) + double.Parse(EdTaxVal.ToString()); 

         TotalDeduction = total.ToString("N2"); 

         NetPay = (double.Parse(Amount.ToString()) - total).ToString("N2"); 
        } 
       } 
       else if(PayeChecked == false) 
       { 
        if (PayeVal != string.Empty) 
        { 
         PayeName = string.Empty; 
         taxable += decimal.Parse(PayeVal.ToString()); 
         StautoryIncome += taxable; 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       var exceptionDialog = new MessageDialog 
       { 
        Message = { Text = ex.ToString() } 
       }; 

       await DialogHost.Show(exceptionDialog, "RootDialog"); 
      } 
     } 

     #endregion 
    } 
} 
+0

MVVM auf separaten UI und Datenschichten gemeint, und Sie sehen, wie Sie sie sind zu kombinieren. Ich habe jetzt keine Zeit, eine vollständige Antwort + Beispiel zu schreiben, aber Sie können ein Beispiel sehen, wie MVVM in einer TabControl-Struktur verwendet wird [hier] (https://rachel53461.wordpress.com/2011/12/18/ navigation-with-mvvm-2 /). Der einzige Ort, an dem ich den DataContext-Satz jemals erwarten würde, ist die anfängliche Bindung für den Anwendungsstart. Irgendwo sonst sendet mir rote Warnflaggen zu. – Rachel

+0

@Rachel, Das von Ihnen angegebene Beispiel ist nicht das, wonach ich suche, und Sie haben Recht mit dem DataContext. Ich habe es für jetzt entfernt – bruh1234

Antwort

0

Sie können das ViewModelLocator-Muster verwenden, um dies zu beheben. Grundsätzlich besteht dieses Muster aus einer Klasse, die den gesamten erforderlichen DataContext als Eigenschaft besitzt. Dies kann statisch sein. Und Sie müssen DataContext nicht auf CodeBehind setzen, Sie können XAML mit Ressourcen festlegen.

Siehe: First
Second

0

Wenn die Registerkarten unterschiedliche Werte anzeigen sollen, müssen Sie natürlich für jedes von Ihnen erstellte TabLayout eine neue Instanz Ihrer Klasse erstellen. Sie könnten die Eigenschaftswerte eine oder andere Weise kopieren:

var workspace = new WorkspaceViewModel 
{ 
    LoadedControl = new TabLayout() { DataContext = new PayslipModel() { Amount = this.Amount, NisName = this.NisName, /* and so on for each property ... */} } 
}; 

Sie können die Datacontext aller Elemente auf der gleichen Instanz festgelegt.

+0

Das ist teilweise das Problem, 'NewWorkspace()' wird im MainViewModel gehalten und PayslipModel ist ein separates ViewModel, das mit was auf Tabs angezeigt wird behandelt. Daher kann ich die Eigenschaften nicht wie vorgeschlagen kopieren. Wenn es eine viel bessere Möglichkeit gibt, Tabs in MVVM zu handhaben, die Sie vielleicht vorschlagen, versuche ich, dann stört es mich nicht :) – bruh1234

+0

Wenn Sie nicht denken, dass jemand irgendwie in der Lage sein wird zu erraten, wie Ihr Code aussieht und was Beziehung zwischen deinen Typen ist und du willst dich immer noch, du solltest das wirklich lesen: http://stackoverflow.com/help/mcve – mm8

+0

Ich habe die Frage aktualisiert. Nur als Nebenbemerkung ist die UserControl Payroll ein Kind des MainWindow, so dass es wie '' referenziert wird und TabLayout ein untergeordnetes Element von Payroll ist, also wenn es Workspace hinzugefügt wird. – bruh1234