Bind GridViewColumnCollection to static resource

1 Answer 37 Views
GridView
Eldoir
Top achievements
Rank 2
Iron
Iron
Iron
Eldoir asked on 21 Feb 2022, 10:37 AM | edited on 21 Feb 2022, 10:40 AM

Hi,

As the title describes it, I'm trying to bind the property "Columns" of a RadGridView to a static resource (could also be bound to a dependency property).

But of course, since this "Columns" property doesn't have a public setter, I can't simply set it with:

<telerik:RadGridView Columns="{Binding InitialColumns}"/>

Nor can I do:

<telerik:RadGridView>
    <telerik:RadGridView.Columns Items="{Binding InitialColumns}"/>
</telerik:RadGridView>

since the "Items" property of Collection<T> doesn't have a public setter either.

As you may have guessed, I'm trying to add a collection of initial columns to the grid. My ultimate goal is to have a template selector that would fill the grid with some initial columns based on some conditions. The rest of the columns would then be added dynamically in code at runtime.

How could I do that? Thanks!

1 Answer, 1 is accepted

Sort by
0
Accepted
Martin Ivanov
Telerik team
answered on 22 Feb 2022, 02:50 PM

Hello Arthur,

Indeed, the Columns property is read-only and it cannot be set. To achieve your requirement, you will need to add columns manually in code-behind (or using a behavior). One way to achieve your requirement would be to get your InitialColumns collection in code-behind and iterate it. Then add each column from your collection to the Columns collection of the RadGridView control. In any case, this will be the main implementation - synchronizing the Columns collection with the one in the view model, using C# (or VB) code.

Regards,
Martin Ivanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Eldoir
Top achievements
Rank 2
Iron
Iron
Iron
commented on 22 Feb 2022, 04:58 PM

Hello Martin,

That's what I did while waiting for your answer and I'm glad it worked and is quite maintainable.

Thanks!

Tags
GridView
Asked by
Eldoir
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Martin Ivanov
Telerik team
Share this question
or