根据UserInterface中的值更改绑定

时间:2015-10-13 08:25:16

标签: c# wpf xaml mvvm data-binding

我正在为传感器实现一个接口。因此,我创建了一个类 TestSensor ,一个ViewModel MainViewModel 和GUI本身。

在GUI中我有ComboBox,它有几个可以显示的单位。标准单元是microstrain,这也是数据读入的单位。 读入数据后,我设法将其转换为所需的其他单位。这些值保存在Sensor对象本身中。

我的问题

我怎么做,标签显示所选单位?因此,当我将单位从 microStrain 更改为例如 ton ,它会将所有标签更新为 ton 中存储的值吗?

我知道转换器,但必须有其他方式,特别是因为我有多个可能的转换!我对XAML和C#比较陌生,所以我真的希望有人能指出一个能让这成为可能的解决方案。

以下是代码:

Class 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:

<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

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

你快到了。对于Label,将Content绑定到ComboBox的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>