Search code examples
c#xamlsilverlighttelerikradgrid

Binding column filter to converted value


 So there is a data model with 2 properties: first name and last name.  

I'm using custom converter to present this value "last name, first name" with logic behind it, that whenever one of the properties is null, converter returns null.
In this situation i also want a telerik filter on column but there is a problem bacause don't bind to converted value but to raw data.

Is there any way to handle this, and have both converted string and filter that is bind to it?

Here's code xaml/silverlight:

<RadGridView ItemsSource="{Binding PersonCollection}">

<telerik:GridViewDataColumn Header="Name" DataMemberBinding="{Binding Converter={StaticResource FirstLastNameConverter}}" /> 

</RadGridView>

Default telerik is not shown because binding here is to whole person object

And Person model (cannot change that or make wrapper around it):

public class Person
{
  public string FirstName { get; set; }
  public string LastName { get; set; }
}

Solution

  • You can use a converter to wrap each item in a custom PersonWrapper type:

    <RadGridView ItemsSource="{Binding PersonCollection,
        Converter={StaticResource ItemWrappingConverter}}">...</...>
    

    and code

    public class ItemWrappingConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object param, CultureInfo cultur)
        {
            var persons = value as IEnumerable<Person>;
            if (persons == null) return null;
            return persons.Select(person => new PersonWrapper()
            {
                Person = person,
                FullName = GetFullName(person.FirstName, person.LastName)
            } );
        }
    
        public object ConvertBack(object value, Type targetT, object param, CultureInfo culture)
        { throw new NotSupportedException(); }
    }
    
    public class PersonWrapper
    {
        public Person Person { get; set; }
        public string FullName { get; set; }
    }