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

Binding a RadGridView to a collection of enums

3 Answers 209 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Matt Greer
Top achievements
Rank 1
Matt Greer asked on 02 Jun 2010, 08:14 PM
I want a RadGridView which simply has a collection of enums as its ItemsSource. I have defined the grid as:

        <gridView:RadGridView x:Name="AssignedPermissionsGrid" AutoGenerateColumns="False" ShowInsertRow="False" ItemsSource="{Binding AssignedPermissions}"
            <gridView:RadGridView.Columns> 
                <gridView:GridViewDataColumn Header="Assigned Permissions" /> 
            </gridView:RadGridView.Columns> 
        </gridView:RadGridView> 

In my code behind I have setup the DataContext of my UserControl to be my ViewModel, and then my ViewModel has a property named AssignedPermissions:

        public ICollection<Permissions> AssignedPermissions 
        { 
            get 
            { 
                return _assignedPermissions; 
            } 
            set 
            { 
                if (value != _assignedPermissions) 
                { 
                    _assignedPermissions = value; 
                    RaisePropertyChanged("AssignedPermissions"); 
                } 
            } 
        } 

Permissions is an enum with the [Flags] attribute.

This all works just fine, except the rows in the grid are empty. So if AssignedPermissions contains 5 elements, then I get five rows in the grid, but each row is blank.

How can I have the value in each row be equal to calling ToString() on the enum value? Or better yet, if I apply System.ComponentModel.DescriptionAttribute to each entry in the enum, how can I get that DescriptionAttribute value be displayed in the grid?

If I bind the grid to an ICollection<int> it displays the int values just fine.

I am using Silverlight 4, .NET 4, and my Telerik.Windows.Controls.GridView assembly is version 2010.1.422.1040

Thanks,


3 Answers, 1 is accepted

Sort by
0
Accepted
Maya
Telerik team
answered on 03 Jun 2010, 12:43 PM
Hi Matt Greer,

You may try to define your grid definition as follows:

<telerik:RadGridView Name="positionsGrid" Grid.Row="1" AutoGenerateColumns="True"
              ItemsSource="{Binding AssignedPositions}">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn  DataMemberBinding="{Binding}">
                </telerik:GridViewDataColumn>
            </telerik:RadGridView.Columns>         
        </telerik:RadGridView>

I am sending you a sample project so that you can see and test the proposed solution. However, in the example, I created another way for using enum in the grid - as a Property a column is bound to.

I hope that helps.
 

Regards,
Maya
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Matt Greer
Top achievements
Rank 1
answered on 04 Jun 2010, 03:56 PM
Weird, that does work. I did try this approach and it didn't work for me, but I guess I didn't quite try it exactly as this and/or had something else set that was breaking it.

Thanks Maya!
0
Matt Greer
Top achievements
Rank 1
answered on 04 Jun 2010, 04:14 PM
Also, in case other people have the same issue, the proper syntax to bind to the enum and use a ValueConverter is this:

DataMemberBinding="{Binding Converter={StaticResource EnumConverter}}" 

Note the lack of a comma between Binding and Converter.
Tags
GridView
Asked by
Matt Greer
Top achievements
Rank 1
Answers by
Maya
Telerik team
Matt Greer
Top achievements
Rank 1
Share this question
or