2017-04-05 88 views
2

我有兩個文本框,如果我更改其中一個值的值,則應計算另一個文本框中的值。這需要雙向工作。WPF在父對象的屬性更改時調用更新源觸發器xaml

在我的ViewModel我有一個名爲NewProductCount

private ProductCount newProductCount; 
    public ProductCount NewProductCount 
    { 
     get 
     { 
      return newProductCount; 
     } 
     set 
     { 
      newProductCount = value; 
      if (newProductCount.PackingUnits != 0 || newProductCount.SellingUnits != 0) 
      { 
       newProductCount.SellingUnits = (int)newProductCount.PackingUnits * SelectedProduct.PurchasePackaging.UnitsPerPackage; 
       newProductCount.PackingUnits = newProductCount.SellingUnits/SelectedProduct.PurchasePackaging.UnitsPerPackage; 
      } 

      NotifyPropertyChanged(); 
     } 
    } 

屬性/對象在我看來(XAML)我有兩個文本框一個StackPanel。 stackpanel的datacontext綁定到ViewModel中的NewProductCount屬性。在這個stackpanel裏面我有兩個文本框。第一個綁定到NewProductCount對象的PackingUnits屬性,第二個綁定到NewProductCount對象的SellingUnits屬性。現在的問題是,當我在其中一個文本框中更改某些內容時,我想要轉到ViewModel中的NewProductCount屬性的setter。

這是我查看樣子:

<StackPanel DataContext="{Binding NewProductCount}" > 
      <Label Content="Number of selling units:"/> 
      <TextBox Text="{Binding SellingUnits}"/> 
      <Label Content="Number of packing units"/> 
      <TextBox Text="{Binding PackingUnits}"/> 
</StackPanel> 

我自己也嘗試updatesourcetrigger(的PropertyChanged)對兩個文本框,但沒有火NewProductCount setter方法在我的視圖模型。

由於提前,

阿恩

+0

它的SellingUnits和PackingUnits性能應該得到的制定者打你的時候鍵入文本框,而不是NewProductCount屬性。沒有爲此提出PropertyChanged事件。 – mm8

+0

我認爲你對NewProductCount的使用方式感到困惑。你目前將它作爲一個StackPanel的DataContext綁定,那麼它是如何設置的? SellingUnits和PackingUnits屬性是否觸發PropertyChanged事件?你的ProductCount類是否實現INotifyPropertyChanged接口?同樣,所有TextBox綁定都應該是Mode = TwoWay來處理對模型屬性和用戶輸入更改的更改。 –

回答

0

你可以訂閱newproductcount對象的PropertyChanged事件(或任何你喜歡的活動,對於這個問題)在您的視圖模型,然後觸發此事件時的一個該對象中的屬性會改變。然後在該事件的處理程序中,您可以觸發newproductcount屬性上的屬性更改事件。

更新: 基於你只是想更新兩個文本框的事實,你只需要做一些改變。爲了清晰:

XAML:

<StackPanel DataContext="{Binding NewProductCount}" > 
     <Label Content="Number of selling units:"/> 
     <TextBox Text="{Binding SellingUnits, UpdateSourceTrigger=PropertyChanged}"/> 
     <Label Content="Number of packing units"/> 
     <TextBox Text="{Binding PackingUnits, UpdateSourceTrigger=PropertyChanged}"/> 
    </StackPanel> 

C#:

public class TestClass 
{ 
    public TestClass() 
    { 
     NewProductCount = new NewProductCount(); 
    } 

    public NewProductCount NewProductCount 
    { 
     get; set; 
    } 
} 

然後:

public class NewProductCount : INotifyPropertyChanged 
{ 
    private string _sUnits; 
    private string _pUnits; 

    public string SellingUnits 
    { 
     get 
     { 
      return _sUnits; 
     } 
     set 
     { 
      _sUnits = value; 
      _pUnits = string.Empty; //do something dumb just to show the bindings are updating... 
      NotifyPropertyChanged(); 
      NotifyPropertyChanged("PackingUnits"); //nameof/reflection/whatever you want to use to pass this property name through. 
     } 
    } 

    public string PackingUnits 
    { 
     get 
     { 
      return _pUnits; 
     } 
     set 
     { 
      _pUnits = value; 
      _sUnits = value; //do something dumb just to show the bindings are updating... 
      NotifyPropertyChanged(); 
      NotifyPropertyChanged("SellingUnits"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    public void NotifyPropertyChanged([CallerMemberName]string propertyName = null) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 
+0

我想不寫任何代碼。這對於學校的任務而言,我們需要將代碼保留爲空。有沒有其他方法可以做到這一點?感謝您的快速回答Jonny。 –

+0

我可以問你爲什麼要調用新產品數量上的propertychanged?只是在更改時刷新包含在堆疊面板中的兩個文本框,還是有其他原因需要執行此操作? – Jonny

+0

沒有,那是原因。 –

相關問題