2015-10-13 66 views
0

我正在實現傳感器接口。因此,我創建了一個類TestSensor,ViewModel MainViewModel和GUI本身。根據UserInterface中的值更改綁定

在GUI中我有ComboBox有幾個單元,它可以顯示。標準單位是微應變,這也是數據讀入的單位。 數據讀入後,我設法將它轉換爲其他需要的單位。這些值保存在Sensor對象本身中。

我現在的問題

我如何能做到這一點,該標籤顯示所選擇的單位?因此,當我將單位從microStrain更改爲ton,它會將所有標籤更新爲ton中存儲的值。

我知道轉換器,但必須有其他方式,尤其是因爲我有多個可能的轉換!我對XAML和C#相對來說比較新,所以我真的希望有人能指出一個解決方案,使之成爲可能。

下面的代碼:

類TestSensor

class TestSensor : ModelBase 
{ 
    double sensorDatauStrain; 
    public double SensorDatauStrain 
    { 
     get { return sensorDatauStrain; } 
     set 
     { 
      if (sensorDatauStrain != value) 
      { 
       sensorDatauStrain = value; 
       this.OnPropertyChanged(); 
      } 
     } 
    } 

    double sensorDatakNewton; 
    public double SensorDatakNewton 
    { 
     get { return sensorDatakNewton; } 
     set 
     { 
      if (sensorDatakNewton != value) 
      { 
       sensorDatakNewton = value; 
       this.OnPropertyChanged(); 
      } 
     } 
    } 

    double sensorDataTon; 
    public double SensorDataTon 
    { 
     get { return sensorDataTon; } 
     set 
     { 
      if (sensorDataTon != value) 
      { 
       sensorDataTon = value; 
       this.OnPropertyChanged(); 
      } 
     } 
    } 

    double sensorDatausTon; 
    public double SensorDatausTon 
    { 
     get { return sensorDatausTon; } 
     set 
     { 
      if (sensorDatausTon != value) 
      { 
       sensorDatausTon = value; 
       this.OnPropertyChanged(); 
      } 
     } 
    } 

    string sensorName; 
    public string SensorName 
    { 
     get { return sensorName; } 
     set 
     { 
      if (sensorName != value) 
      { 
       sensorName = value; 
       this.OnPropertyChanged(); 
      } 
     } 
    } 

    public TestSensor(string name, double ustrain) 
    { 
     this.SensorName = name; 
     this.SensorDatauStrain = ustrain; 
    } 

    public void UpdateUnits(double emodule, double diameter) 
    { 
     SensorDatakNewton = (SensorDatauStrain * 1000000)/(emodule * diameter); 
     SensorDataTon = 9.81 * SensorDatakNewton; 
     SensorDatausTon = 1.1 * SensorDatausTon; 
    } 
} 

在VM代表的傳感器:

public MainViewViewModel() 
    { 
     Sensor11 = new TestSensor("Sensor 1.1", 0); 
     Sensor12 = new TestSensor("Sensor 1.2", 0); 
     Sensor21 = new TestSensor("Sensor 2.1", 0); 
     Sensor22 = new TestSensor("Sensor 2.2", 0); 
     Sensor31 = new TestSensor("Sensor 3.1", 0); 
     Sensor32 = new TestSensor("Sensor 3.2", 0); 
     Sensor41 = new TestSensor("Sensor 4.1", 0); 
     Sensor42 = new TestSensor("Sensor 4.2", 0); 
     SideFactors = new Factors(100, 210); 
     LimitPos = 0; 
     LimitNeg = 0; 
    } 

    //-----------------Chosen unit------------------ 
    Unit unit; 
    public Unit Unit 
    { 
     get { return unit; } 
     set 
     { 
      if (unit != value) 
      { 
       unit = value; 
       this.OnPropertyChanged(); 
      } 
     } 

    } 



    //----------------One of the Sensors------------------------------ 
    TestSensor sensor11; 
    public TestSensor Sensor11 
    { 
     get { return sensor11; } 
     set 
     { 
      if (sensor11 != value) 
      { 
       sensor11 = value; 
       this.OnPropertyChanged(); 
       this.OnPropertyChanged("SubTotal1"); 
       this.OnPropertyChanged("Average"); 
      } 
     } 
    } 

而且繼承人的XAML碼:

標籤:

<Label Grid.Column="0" Grid.Row="1" Margin="0,0,1,1" FontSize="8" Content="{Binding Sensor11.SensorDatauStrain, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" /> 

組合框:

<ComboBox x:Name="comboBox" IsReadOnly="True" VerticalAlignment="Center" 
            ItemsSource="{Binding Source={data:EnumBindingSource {x:Type data:Unit}}}" SelectedItem="{Binding Unit, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> 

這是GUI的樣子:

on the top left corner you see sensor 1.1, then sensor 1.2 etc. this is the place where the value of the Sensor 1.1 should be visible IN the chosen unit

感謝您的幫助!

回答

0

你快到了。對於標籤,將內容綁定到組合框的SelectedValue屬性,而不是將其綁定到Sensor11

例如:

<Label Margin="86,48,0,0" 
     HorizontalAlignment="Left" 
     VerticalAlignment="Top" 
     Content="{Binding SelectedValue, 
         ElementName=comboBox}" /> 
<ComboBox x:Name="comboBox" 
      Width="120" 
      Margin="94,21,0,0" 
      HorizontalAlignment="Left" 
      VerticalAlignment="Top" 
      SelectedIndex="0"> 
      <!-- ... ---> 
</ComboBox> 
+0

你的意思是這樣 '含量= {x:Type數據:單位}'?這只是在標籤顯示「Namespace.Data.Unit」... 我需要程序來改變綁定表達式取決於所選單元! – Laurence

+0

@Laurence對不起。我誤寫了,因爲我複製了一段錯誤的代碼!我用適當的代碼更新了我的答案,而不是混淆的單詞。 :)本質上,標籤採用組合的選定值。 –

+0

現在我明白你的意思了,但有一個普遍的誤解。我更新了信息......如果你可以再次檢查,那將會很棒。 – Laurence