Silverlight Feeds - All your Silverlight feeds in one place.

Sponsors

Tuesday, July 21, 2009

Silverlight multi-binding for WPF

by StefanOlson via Stefan Olson's Blog on 7/21/2009 10:42:13 AM

In my previous post on Silverlight multi-binding support I mentioned that theoretically it should be possible to use the Silverlight multi-bindings in WPF. Unfortunately the practicalities of single sourcing between WPF and Silverlight are not always as simple as you might expect, and this case was no different!

Given that WPF already has its own multi-binding functionality, why would you want to use the Silverlight attack? You only really want to do this when you want to have source code compatibility between WPF and Silverlight. The updated source code for the Silverlight multi-bindings, which is available below demonstrates this with an application that uses the same source code for both WPF and Silverlight.

So onto the difficulties in making it work for WPF. The first problem is that Colin’s original structure used an ObservableCollection for the bindings:

<local:MultiBinding TargetProperty="Text" Converter="{StaticResource TitleConverter}">
    <Binding Path="Surname"/>                            
    <Binding Path="Forename"/>
</local:MultiBinding>

Unfortunately,WPF will not let you put bindings into an ObservableCollection, so I had to build a new BindingCollection class based on the model used by multi-bindings in WPF.

Once that was solved the larger problem was the fact that objects are created differently from the Xaml/Baml between Silverlight and WPF. This meant the assumptions about the timing of the creation and the completeness of an object at any given time were wrong.  WPF’s Baml reader will create an object, assign it to a dependency property and then fill its attributes. Silverlight’s Xaml reader will create an object, fill it’s attributes and then assign it to the dependency property, a feature the Colin was relying on.

So I had to come up with a completely different solution. What I decided to do was catch the loaded event of the object that we are connected to and at that time I convert the bindings that we have created in to pure WPF multi-bindings as you can see in the code below:

#if !SILVERLIGHT
void Loaded(object sender, RoutedEventArgs e)
{
    _targetElement.Loaded -= Loaded;
    foreach (MultiBinding binding in Bindings)
    {
        FieldInfo field = _targetElement.GetType().GetField(binding.TargetProperty + "Property",
                                                            BindingFlags.Public | BindingFlags.Static |
                                                            BindingFlags.FlattenHierarchy);
        if (field == null) continue;

        System.Windows.Data.MultiBinding newBinding = new System.Windows.Data.MultiBinding
                                                          {
                                                              Converter = binding.Converter,
                                                              ConverterParameter = binding.ConverterParameter
                                                          };
        foreach (BindingBase bindingBase in binding.Bindings)
        {
            newBinding.Bindings.Add(bindingBase);
        }
        
        DependencyProperty dp = (DependencyProperty)field.GetValue(_targetElement);

        BindingOperations.SetBinding(_targetElement, dp, newBinding);
    }
}
#endif

The only tricky part is finding the dependency property that we need to work with - This is a bit of a hack where I search for a static field with the given name and Property appended to it, which is the naming convention for dependency properties.

Using the multi-bindings is still exactly the same as described in the previous post but can now be used on both WPF and Silverlight!

Download Source

you can download the latest source code from here

…Stefan

email it!bookmark it!digg it!

Original Post: Silverlight multi-binding for WPF

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. Silverlight Feeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on Silverlight Feeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us