Multibinding

8 posts, 1 answers
  1. david ocasio
    david ocasio avatar
    22 posts
    Member since:
    Apr 2006

    Posted 09 Oct 2009 Link to this post

    Is it possible to combine values from two columns to show one column
    i.e

    if the data  has a "first name" field  and a "last name" fiel
    i want to show in the grid a column called "fullname" which is the combination of those two

    i can make an sql view with the combination of those columns to feed to the grid but id rather not

    thanks
    dco


  2. Answer
    Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 13 Oct 2009 Link to this post

    Hi david ocasio,

    You can easily do this by using an IValueConverter. Create a binding to the business object itself and the in the converter return the concatenated value like this:

    <UserControl x:Class="TicketID_249361_Multibinding.MainPage"
        xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
        xmlns:local="clr-namespace:TicketID_249361_Multibinding"
        mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
        <Grid>
            <Grid.Resources>
                <local:ClubToInfoConverter x:Key="ClubToInfoConverter"/>
            </Grid.Resources>
            <telerik:RadGridView Name="clubsGrid" Grid.Row="0" AutoGenerateColumns="False"
                    ColumnsWidthMode="Auto">
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn Header="Name" DataMemberBinding="{Binding Name}">
                    </telerik:GridViewDataColumn>
                    <telerik:GridViewDataColumn Header="Est." DataMemberBinding="{Binding Established}"
                            DataFormatString="{}{0:yyyy}">
                    </telerik:GridViewDataColumn>
                    <telerik:GridViewDataColumn Header="Stadium"
                            DataMemberBinding="{Binding StadiumCapacity}" DataFormatString="{}{0:N0}">
                    </telerik:GridViewDataColumn>
                    <telerik:GridViewDataColumn Header="Info"
                            DataMemberBinding="{Binding Path=., Converter={StaticResource ClubToInfoConverter}}">
                    </telerik:GridViewDataColumn>
                </telerik:RadGridView.Columns>
            </telerik:RadGridView>
        </Grid>
    </UserControl>

    And the converter that does the magic:

    using System;
    using System.Globalization;
    using System.Windows.Data;
     
    namespace TicketID_249361_Multibinding
    {
        public class ClubToInfoConverter : IValueConverter
        {
            #region IValueConverter Members
     
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                var club = (Club) value;
                return string.Format("{0} was founded in {1}."
                                     , club.Name
                                     , club.Established.Year);
            }
     
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
     
            #endregion
        }
    }

    I have attached a sample project. I hope this helps.

    Sincerely yours,
    Ross
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. DevCraft banner
  4. david ocasio
    david ocasio avatar
    22 posts
    Member since:
    Apr 2006

    Posted 13 Oct 2009 Link to this post

    thanks
    that works great

    thats the concept i was missing
    passing the datacontext to the converter 
    with the "path=."

    again it works great
    thankyou very much
  5. Pankaj
    Pankaj avatar
    15 posts
    Member since:
    May 2011

    Posted 25 May 2011 Link to this post

    Hi Ross,

    I tried your solution but it gave me object reference error. 'value' arguement  was set to null. Please suggest.
  6. Lauren
    Lauren avatar
    37 posts
    Member since:
    Jan 2010

    Posted 27 Sep 2011 Link to this post

    Hi,

    I'm trying to do something very similar:
    my radgridview's itemsource = List<Object>
    Let's say Object has 4 properties: p1, p2, p3, p4
    p1 and p2 are directly bound to two columns.
    for the 3rd column, depending on teh string value of p3, i want to display p4, else i want to keep the cell empty.

    I'mdoing what described in a previous post
    DataMemberBinding="{Binding Path=., Converter={StaticResource ClubToInfoConverter}}"
    but the following exception keeps coming: "two way binding requires path"

    How can i solve this ?
    thanks.

  7. Daní
    Daní avatar
    303 posts
    Member since:
    Feb 2008

    Posted 28 Sep 2011 Link to this post

    My two cents about the solution supplied by Ross:

    The solution will work fine only if the properties involved are constant, so they won't never change. In fact, it is not realizing real multibinding. The binding won't never be aware for changes in "Name" or "Stablished.Year" properties. This approach is usefull in several scenarios like this, but unusefull in many others. Silverlight lacks for multibinding support built in, however, fortunately, you can find many MultiBinding implementations for silverlight just googling that can solve all scenarios. I have my own multibinding implementation based on http://www.scottlogic.co.uk/blog/colin/2010/05/silverlight-multibinding-solution-for-silverlight-4/ that I can share with every one interested.
  8. Lauren
    Lauren avatar
    37 posts
    Member since:
    Jan 2010

    Posted 28 Sep 2011 Link to this post

    Thanks Dani.
    You're right, i hink the issue here is that i want to be able to insert rows into my radgridview.
    Binding works the first time, but when i add a new row and insert a value for p4, it fails with the exception "two way binding requires path"

    Any solution to achieve this with RadGridView ?
    Thank you.
  9. Daní
    Daní avatar
    303 posts
    Member since:
    Feb 2008

    Posted 28 Sep 2011 Link to this post

    Hi Lauren,

    Multibing is complex and requires loads of stuff (xaml + IMultivalueConverter). I only use Multibinding as the last option. I don't know how your scenario exactly is. In many situations similars to the one you expose I often add an extra property, let's call p5. p5 is a readonly property that returns p4 o null depending on p3 value. Below I'm pasting an example:
    public class MyViewModel: ViewModelBase
        {
            private string _p3;
                     
            public string p3
            {
                get
                {
                    return this._p3;
                }
                set
                {
                    if (this._p3 != value)
                    {
                        this._p3 = value;
                        this.OnPropertyChanged("p3");
                        this.OnPropertyChanged("p5");
                    }
                }
            }
     
            private string _p4;
                     
            public string p4
            {
                get
                {
                    return this._p4;
                }
                set
                {
                    if (this._p4 != value)
                    {
                        this._p4 = value;
                        this.OnPropertyChanged("p4");
                    }
                }
            }
     
            public string p5
            {
                get { return string.IsNullOrEmpty(p3) ? string.Empty : p4; }
            }
        }

    Now you can bind the 3rd column to p5 property. Whenever p3 property changes it raises the PropertyChanged event for both p3 and p5 properties, so UI will be notified. 
Back to Top
DevCraft banner