Click here to Skip to main content
15,880,608 members
Articles / Web Development / HTML

MultiBinding in Silverlight 5

Rate me:
Please Sign up or sign in to vote.
4.97/5 (23 votes)
18 Nov 2011CPOL12 min read 134.9K   3.2K   33  
An enhanced MultiBinding markup extension implementation for Silverlight 5 with support for bindable Converter, ConverterParameter and StringFormat
<UserControl x:Class="SilverlightMarkupExtensions.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:z="clr-namespace:SilverlightMarkupExtensions"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
        <z:StringFormatMultiValueConverter x:Key="stringFormatConverter" />
        <Style x:Key="myStyle1" TargetType="TextBox">
            <Setter Property="Text" Value="{z:MultiBinding Mode=TwoWay, Source1={Binding Title, Mode=TwoWay}, Converter={StaticResource stringFormatConverter}}" />
            <Setter Property="Tag" Value="{z:MultiBinding Source1={Binding Title}, Converter={StaticResource stringFormatConverter}}" />
        </Style> 
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.DataContext>
            <z:MyModelView />
        </Grid.DataContext>
        <StackPanel>
            
          <!--  <TextBlock Text="{z:MultiBinding Source1={Binding Title}}" /> 
            <TextBlock Name="title" Text="Title" />
            <TextBlock Text="{z:MultiBinding Source1={Binding Title}}" />  -->
            <TextBlock FontWeight="Bold">MultiBinding in ListBox' ItemsTemplate with StringFormat bound to ComboBox. </TextBlock>
            <StackPanel Orientation="Horizontal" >
                <TextBlock Text="Name format: " />
                <ComboBox Name="cboNameFormat" Width="180" SelectedValuePath="Tag" >
                    <ComboBoxItem Tag="%0 %1" IsSelected="True">First and last name</ComboBoxItem>
                    <ComboBoxItem Tag="%1, %0">Last name, first name</ComboBoxItem>
                </ComboBox>
            </StackPanel>
            <ListBox ItemsSource="{Binding Persons}" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock>
                            <TextBlock.Text>
                                <z:MultiBinding StringFormat="{Binding SelectedValue, ElementName=cboNameFormat}" >
                                    <z:BindingCollection>
                                        <Binding Path="FirstName" />
                                        <Binding Path="LastName" />
                                    </z:BindingCollection>
                                </z:MultiBinding>
                                </TextBlock.Text>
                        </TextBlock>
                    </DataTemplate>    
                </ListBox.ItemTemplate>    
            </ListBox> 
            <TextBlock FontWeight="Bold">MultiBinding applied via Style</TextBlock>
            <TextBox Style="{StaticResource myStyle1}" /> 
          <!--  <StackPanel DataContext="{x:Null}" >
                <TextBlock  Style="{StaticResource myStyle1}" />
            </StackPanel> -->
            <TextBlock FontWeight="Bold">MultiBinding with bindings to named elements.</TextBlock>
            <CheckBox Name="chkLicenceAccepted" >I have accepted the Licence</CheckBox>
            <CheckBox Name="chkConditionsAccepted" >I have accepted the Conditions</CheckBox>
            <Button Content="Continue" >
                <Button.IsEnabled>
                    <z:MultiBinding Source1="{Binding IsChecked, ElementName=chkLicenceAccepted}" 
                                        Source2="{Binding IsChecked, ElementName=chkConditionsAccepted}" 
                                        Converter="{z:MinNumberOfSetConverter}"
                                        ConverterParameter="2"
                                        />
                </Button.IsEnabled>
            </Button>
            <TextBlock FontWeight="Bold">MultiBinding with two-way binding to two model view properties.</TextBlock>  
            <StackPanel Orientation="Horizontal" >
                <TextBlock Text="Length: " />
                <TextBox Width="100" BindingValidationError="TextBox_BindingValidationError" >
                    <TextBox.Text>
                        <z:MultiBinding Mode="TwoWay" 
                                            NotifyOnValidationError="True" ValidatesOnExceptions="true" 
                                            Converter="{z:LengthConverter}"
                                            Source1="{Binding Length, Mode=TwoWay, ValidatesOnExceptions=true}"
                                            Source2="{Binding LengthUnit, Mode=TwoWay, ValidatesOnExceptions=true}"  />
                    </TextBox.Text>
                </TextBox> 
                <TextBlock Text="{z:MultiBinding Source1={Binding Length},Source2={Binding LengthUnit},StringFormat='%0 %1'}" />
            </StackPanel>
            <TextBlock FontWeight="Bold" >MultiBinding used with a IValueConverter:</TextBlock>
            <TextBox Text="{z:MultiBinding Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, Source1={Binding Title,Mode=TwoWay},Converter={z:ReverseStringConverter}}" />
        </StackPanel>
    </Grid>
</UserControl>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Sweden Sweden
Henrik Jonsson is a Microsoft Professional Certified Windows Developer (MCPD) that currently works as an IT consultant in Västerås, Sweden.

Henrik has worked in several small and large software development projects in various roles such as architect, developer, CM and tester.

He regularly reads The Code Project articles to keep updated about .NET development and get new ideas. He has contributed with articles presenting some useful libraries for Undo/Redo, Dynamic Linq Sorting and a Silverlight 5 MultiBinding solution.

Comments and Discussions