WPF Multicast Binding

Requirement:

A single control (in the example, ScriptExecutor) needs to set appearance of two different controls (a checkBox and a progress bar).

Normally we would set checkbox and progressbar from ScriptExecutor but in this scenario, we need to be able to use multiple scripts, so ScriptExecutor needs to “publish” its values.

Solution:

1. We need to use MultiBinding with a MultiValueConverter as shown below:

public class MulticastBindingConverter : IMultiValueConverter
{
    private int Length;

    #region IMultiValueConverter Members

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values != null && values.Length > 0)
        {
            Length = values.Length;
            return values[0];
        }
        return null;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        if (Length != 0)
        {
            object[] values = new object[Length];

            for (int i = 0; i < Length; i++)
            {
                values[i] = value;
            }

            return values;
        }

        return new object[0];
    }

    #endregion IMultiValueConverter Members
}

2. Import the multivalueconverter into xaml

<Page x:Class="EventMonitor.Controls.Windows.ServerRolesWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:EventMonitor.DataPresenters.Converters;assembly=EventMonitor.DataPresenters" DataContext="{Binding RelativeSource={RelativeSource Self}}"
             xmlns:this="clr-namespace:EventMonitor.Controls.Windows"
             xmlns:config="clr-namespace:EventMonitor.DataPresenters.Configuration;assembly=EventMonitor.DataPresenters"
             xmlns:usercontrol="clr-namespace:EventMonitor.Controls.UserControls"
             mc:Ignorable="d" >
   <local:MulticastBindingConverter x:Key="MulticastBinder"/>

3. Set the MultiBinding on the property.

            <usercontrol:ScriptExecutor ScriptFile="C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" ScriptArgs="-R"  x:Name="ScriptExecutor" ConsoleText="{Binding Mode=OneWayToSource, ElementName=ConsoleTextBox, Path=Text}">
                <usercontrol:ScriptExecutor.IsRunning>
                    <MultiBinding Mode="OneWayToSource" Converter="{StaticResource ResourceKey=MulticastBinder}">
                        <Binding Mode="OneWayToSource" ElementName="LoadingControl" Path="IsLoading"/>
                        <Binding Mode="OneWayToSource" ElementName="ShowConsoleCheckBox" Path="IsChecked"/>
                    </MultiBinding>
                </usercontrol:ScriptExecutor.IsRunning>
            </usercontrol:ScriptExecutor>

 

Related Links:

http://stackoverflow.com/questions/848946/is-it-possible-to-bind-wpf-combobox-selectedvalue-to-multiple-objectdataprovider

Advertisements

About vijayvepa

I'm a software consultant for Software Specialists Inc. currently working at Philips Respironics, Pittsburgh

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: