开发者

How flexible is DataBinding in WPF?

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.


how flexible? so flexible it hasn't failed once for me.

you can do pretty much anything with WPF's bindings. You can bind double values to strings by using Converters. you can get a value from multiple bindings (MultiBinding). You don't have to worry about change propagation.

As this sounds like a decision for/against the use of WPF depending on how capable binding is, worry not! you can do anything!

If you have a specific problem, please post a specific question.


Databinding in WPF is much better than databinding in Winforms, if that's what you're asking about?

Not only are there more options for easy conversion, as suggested by Markus, there are fewer edge cases to worry about, it is more likely to Just Work.


Once you learn the limitations, WPF's binding system is pretty flexible.

Any type can be bound to any other type. If the types don't have an obvious conversion then you can use an IValueConverter to convert from one type to another.

The limitations I've found so far are:

  • Cannot bind to a method. Bindings can only happen between properties.

  • You can only bind to DependencyProperties on controls. Most properties are DependencyProperties, but occasionally they are not, which can be frustrating.

  • Things can become complex when an elements DataContext is set by WPF and a child element wants access to the original DataContext. For example, in a ListBox each item's DataContext is set to the item in the ListBox. So if you want the DataContext of the ListBox you have to jump through some hoops.

  • Another binding issue comes from items that are not part of the visual tree, like ContextMenus. Similar to items in a ListBox you have to jump through a few hoops to get them to work properly.

Most people implement INotifyPropertyChanged to allow WPF to watch for changes to their bound objects. But you can also use DependencyProperties so long as your object inherits from DependencyObject.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜