Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You generally can do such things by using <code>IValueConverter</code>. </p> <p>When you create a proper converter you can specify to use it for the binding in the XAML. </p> <p>For the "real time" part, as far as you implement INotifyPropertyChanged properly in your DataContext object, modifying the variable will be reflected on the UI, applying all the value converter you decided.Lets'have an example using a slider as you said:</p> <pre><code>&lt;Window x:Class="Example.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Example" Title="MainWindow" Height="350" Width="525" &gt; &lt;Window.Resources&gt; &lt;local:HMLConverter x:Key="hiloConverter"&gt;&lt;/local:HMLConverter&gt; &lt;/Window.Resources&gt; &lt;Slider Value="{Binding MyValue, Converter={StaticResource hiloConverter}}" Maximum="100" Minimum="0"/&gt; &lt;/Window&gt; </code></pre> <p>I created a little converter, called HMLConverter, than maps a string variable to the value 0-50-100 for the respective "lo","med",ang "hi" strings. The converter is declared as a static resource, and used in the binding. Converter code looks like:</p> <pre><code>namespace Example { public class HMLConverter:IValueConverter { #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { string val = value.ToString(); int ret = 0; switch (val.ToLower()) { case "hi": ret = 100; break; case "lo": ret = 0; break; case "med": ret=50; break; default: throw new NotSupportedException("Value " + val + " is not supported"); } return ret; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } #endregion } } </code></pre> <p>As you probably guess, I wrote it only in one direction, so the binding does not works in both way, but I'm sure you got the whole part.</p>
 

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