WPF Multicast Binding


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.


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:local="clr-namespace:EventMonitor.DataPresenters.Converters;assembly=EventMonitor.DataPresenters" DataContext="{Binding RelativeSource={RelativeSource Self}}"
             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}">
                    <MultiBinding Mode="OneWayToSource" Converter="{StaticResource ResourceKey=MulticastBinder}">
                        <Binding Mode="OneWayToSource" ElementName="LoadingControl" Path="IsLoading"/>
                        <Binding Mode="OneWayToSource" ElementName="ShowConsoleCheckBox" Path="IsChecked"/>


Related Links:



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: