How flexible is DataBinding in WPF? I am new to WPF and would like to have a clear idea. Can I Bind a string variable with a Slider value? Does it always have to be a control?
Is it at all possible to bind a string value with a Slider control so that it updates in real time? How far up the Logical tree does the update travel or do we have to reissue all the commands to use this new value?
That was my hurried question earlier. I am posting a detailed problem.
What I am trying to do is, I have an application with multiple user-controls. These user controls are grid-views and contain controls (TextBox, Button) and sections of my application UI. I used this approach to split my main XAML file across multiple files and it works for me till now. Now, I have a Slider in one user control and want to bind a String variable in another user control. The string variable is read-only so there is no use for two-way binding.
I hope that makes my question clear. In my code, I am trying to update the variable zoomFactor in:
URLQuery.Append("&zoom=" + zoomFactor + "&size=600x500&format=jpg&sensor=false");
with a Slider control. For those who could spot that, yes, it is the Google Static Maps API and yes, I am trying to zoom into the map with a slider.
You generally can do such things by using IValueConverter
.
When you create a proper converter you can specify to use it for the binding in the XAML.
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:
<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"
>
<Window.Resources>
<local:HMLConverter x:Key="hiloConverter"></local:HMLConverter>
</Window.Resources>
<Slider Value="{Binding MyValue, Converter={StaticResource hiloConverter}}" Maximum="100" Minimum="0"/>
</Window>
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:
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
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With