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