Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could use a MultiBinding that is connected with the two values of the sensor and a third property to determine the current value:</p> <pre><code>&lt;TextBlock&gt; &lt;TextBlock.Resources&gt; &lt;y:SensorValueConverter x:Key="SVC"&gt;&lt;/y:SensorValueConverter&gt; &lt;/TextBlock.Resources&gt; &lt;TextBlock.Text&gt; &lt;MultiBinding Converter="{StaticResource SVC}"&gt; &lt;Binding Path="MyModel.BarometerSensor.HighVal" /&gt; &lt;Binding Path="MyModel.BarometerSensor.LowVal" /&gt; &lt;Binding Path="MyModel.BarometerSensor.SelectedVal" /&gt; &lt;/MultiBinding&gt; &lt;/TextBlock.Text&gt; &lt;/TextBlock&gt; </code></pre> <p>In the Windows tag you specify the namespace of the converter, but you have to replace the "YOUR-PROJECTNAME" placeholder:</p> <pre><code>&lt;Window x:Class="..." ... xmlns:y="clr-namespace:YOUR-PROJECTNAME"&gt; </code></pre> <p>I have extended the class Sensor by the property SelectedVal which stores the value that should be shown in the TextBox. Changing it to "High" or to "Low" will immediatly update the Binding of the TextBox.</p> <pre><code>public enum SelectedValue { High, Low } public class Sensor : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public int HighVal {get; set;} public int LowVal {get; set;} SelectedValue selval = SelectedValue.High; public SelectedValue SelectedVal { get { return selval; } set { selval = value; PropertyChanged(this, new PropertyChangedEventArgs("SelectedVal")); } } } </code></pre> <p>And now the most important part, the ValueConverter for the Binding that will choose the right value depending on the state of SelectedVal (class <code>CultureInfo</code> is the System.Globalization namespace):</p> <pre><code>public class SensorValueConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { SelectedValue val = (SelectedValue)values[2]; if (val == SelectedValue.High) { return values[0].ToString(); } else { return values[1].ToString(); } } public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } </code></pre> <p>To change the selected value of the sensor, you need to modify the SelectedVal property:</p> <pre><code>MyModel.BarometerSensor.SelectedVal = SelectedValue.Low </code></pre> <p><strong>UPDATE:</strong><br /> To assign the binding to many Elements, a style can be defined in a separate file (Styles.xaml):</p> <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:y="clr-namespace:YOUR-PROJECTNAME"&gt; &lt;y:SensorValueConverter x:Key="SVC"&gt;&lt;/y:SensorValueConverter&gt; &lt;Style x:Key="ValStyle" TargetType="TextBlock"&gt; &lt;Setter Property="Text"&gt; &lt;Setter.Value&gt; &lt;MultiBinding Converter="{StaticResource SVC}"&gt; &lt;Binding Path="MyModel.BarometerSensor.HighVal" /&gt; &lt;Binding Path="MyModel.BarometerSensor.LowVal" /&gt; &lt;Binding Path="MyModel.BarometerSensor.SelectedVal" /&gt; &lt;/MultiBinding&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/ResourceDictionary&gt; </code></pre> <p>To use this Style from other windows put simply this line in the resources of the window:</p> <pre><code>&lt;ResourceDictionary Source="Styles.xaml"&gt;&lt;/ResourceDictionary&gt; </code></pre> <p>Finally you need to assign the created style to all TextBlocks that should display the values:</p> <pre><code>&lt;TextBlock Style="{StaticResource ValStyle}"&gt;&lt;/TextBlock&gt; </code></pre> <p>For a sensor with only one property, you can create another style in Styles.xaml that has to be bound to only one property:</p> <pre><code>&lt;Style x:Key="SpecialValStyle" TargetType="TextBlock"&gt; &lt;Setter Property="Text"&gt; &lt;Binding Path="MyModel.BarometerSensor.HighVal" /&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload