This is a migrated thread and some comments may be shown as answers.

Filtering of GridViewComboBoxColumn using enums and DisplayMemberPath

1 Answer 457 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Christian
Top achievements
Rank 1
Christian asked on 27 Jan 2017, 04:57 PM

Hi @ll,

 

I need your help. After searching the internet for several hours I have
no solution for my problem.

 

I have an enum property which I bind to a GridViewComboBoxColumn
using the following xaml code:

<telerik:GridViewComboBoxColumn ItemsSourceBinding="{Binding Options}"
        DataMemberBinding="{Binding Position}"
        DisplayMemberPath="Value"
        SelectedValueMemberPath="Key"/>

 

Options is a property which maps the enum value to a string:

public enum Pos
{
    A, B, C
}
 
public class DisplayObject<T>
{
    private readonly T key;
 
    private readonly string val;
 
    public DisplayObject(T key, string val)
    {
        this.key = key;
        this.val = val;
    }
 
    public T Key => this.key;
 
    public string Value => this.val;
}

 

public IEnumerable<DisplayObject<Pos>> Options
{
    get
    {
        yield return new DisplayObject<Pos>(Pos.A, "HUGO");
        yield return new DisplayObject<Pos>(Pos.B, "EGON");
        yield return new DisplayObject<Pos>(Pos.C, "WILLI");
    }
}

 

Grid works fine. But there are some problems with the FilteringControl.

I want the FilteringControl to uses the DisplayMemberPath to display
the DistinctValues and ComboBox Options.

 

Can anyone help me solving my problem?

 

public class Item : INotifyPropertyChanged
{
    private Pos pos;
 
    private string name;
 
    public Item(string name, Pos pos)
    {
        this.name = name;
        this.pos = pos;
    }
 
    public string Name
    {
        get
        {
            return this.name;
        }
    }
 
    public Pos Position
    {
        get
        {
            return this.pos;
        }
 
        set
        {
            if (this.pos == value)
            {
                return;
            }
 
            this.pos = value;
            this.OnPropertyChanged(nameof(this.Position));
        }
    }
 
    public IEnumerable<DisplayObject<Pos>> Options
    {
        get
        {
            yield return new DisplayObject<Pos>(Pos.A, "HUGO");
            yield return new DisplayObject<Pos>(Pos.B, "EGON");
            yield return new DisplayObject<Pos>(Pos.C, "WILLI");
        }
    }
 
    public event PropertyChangedEventHandler PropertyChanged;
 
    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged(string propertyName)
    {
        this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
<Window x:Class="ChangeDefaultFilterOperator.MainWindow"
        xmlns:my="clr-namespace:ChangeDefaultFilterOperator"
        Title="MainWindow" Height="700" Width="700">
    <Window.Resources>
        <my:MyViewModel x:Key="MyViewModel"/>
    </Window.Resources>
    <Grid DataContext="{StaticResource MyViewModel}">
        <telerik:RadGridView ItemsSource="{Binding Items}"
                             AutoGenerateColumns="False"
                             Margin="5">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}"/>
 
                <telerik:GridViewComboBoxColumn ItemsSourceBinding="{Binding Options}"
                                                DataMemberBinding="{Binding Position}"
                                                DisplayMemberPath="Value"
                                                SelectedValueMemberPath="Key"/>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>
</Window>
public class MyViewModel
{
    private readonly ObservableCollection<Item> items;
 
    public MyViewModel()
    {
        this.items = new ObservableCollection<Item>
                     {
                         new Item("AAA", Pos.A),
                         new Item("BBB", Pos.B),
                         new Item("CCC", Pos.C)
                     };
    }
 
    public ObservableCollection<Item> Items
    {
        get
        {
            return this.items;
        }
    }
}

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 01 Feb 2017, 09:05 AM
Hello Christian,

Thank you for the provided code.

I have already replied you in the support thread you opened regarding this topic. I will paste it here as well, so that it is shared with the community.

Thank you for the provided sample application.

After reviewing it, I noticed that the assembly version you are using is quite outdated(2010.2.924). I strongly recommend you to update to the latest official release, if possible. By doing so, you will be able to utilize the FilterMemberPath property of GridViewComboBoxColumn. Generally, with such approach the column will be filtered over a property different from the one it is bound to. Also, the values displayed in the DistinctValues list will be the values of the property the FilterMemberPath points to.

Can you please give the suggestion a try and let me know whether this corresponds to your requirements?

Additionally, you may find the Basic Filtering topic useful on this matter.

If further assistance is required or you have any concerns, I would kindly ask you to continue our communication in the support ticket. Thank you in advance for your cooperation.

Regards,
Stefan X1
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
GridView
Asked by
Christian
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or