2015-10-13 65 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