How to set column chooser fixed value

1 Answer 197 Views
GridView
Psyduck
Top achievements
Rank 5
Bronze
Bronze
Bronze
Psyduck asked on 03 Jan 2022, 09:33 AM

Hello.

 

I'm trying to use the "Column Chooser" from the telerik SDK.

The SDK is in a state where both ID and Name can be controlled Visible.

I want to change a specific column to be fixed.

For example, ID and Name in the SDK example. I want only Name to be IsVisible, excluding ID from Listbox.
(ID is always shown)

Is it possible to simply control it in xaml? Or do I need extra work in the viewmodel?

I'm using the "declarative column definition method using the DataMemberBinding property set".

 

thanks.

 

1 Answer, 1 is accepted

Sort by
1
Accepted
Dilyan Traykov
Telerik team
answered on 04 Jan 2022, 12:16 PM

Hello Psyduck,

You can achieve the desired result by defining a similar converter for the Columns binding of the ItemsSource of the ListBox:

    public class ColumnChooserConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            var columns = values[0] as Telerik.Windows.Controls.GridViewColumnCollection;
            var filteredColumns = columns.OfType<GridViewBoundColumnBase>()
                .Where(c => c.DataMemberBinding.Path.Path != "ID");
            return filteredColumns;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

Of course, you can replace the Where clause with your custom predicate.

The binding can in turn be defined as follows:

            <ListBox.ItemsSource>
                <MultiBinding Converter="{StaticResource ColumnChooserConverter}">
                    <Binding Path="Columns" ElementName="RadGridView1" />
                    <Binding Path="Columns.Count" ElementName="RadGridView1" />
                </MultiBinding>
            </ListBox.ItemsSource>

You may note that I've used a MultiBinding and also bound the Columns.Count property. This is required in the case where the columns are automatically generated which does not fit the scenario you described but I wanted to demonstrate it for completeness. You can use a standard IValueConverter and only the first binding if the columns collection will not be changed dynamically.

Please also refer to the attached project which demonstrates this approach in action. Do have a look and let me know if this would work for you.

Regards,
Dilyan Traykov
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/.

Psyduck
Top achievements
Rank 5
Bronze
Bronze
Bronze
commented on 05 Jan 2022, 01:19 AM

Hello.

Thanks for attaching the example project.

I have to hide some columns, so I processed it as follows and it works fine.

var columns = values[0] as Telerik.Windows.Controls.GridViewColumnCollection;
var filters = new List<string> { "ID", "TestColumn3", "TestColumn5" };
ar filteredColumns = columns.OfType<GridViewBoundColumnBase>().Where(x => !filters.Any(y => y.Equals(x.DataMemberBinding.Path.Path)));
eturn filteredColumns;
Thanks.
Tags
GridView
Asked by
Psyduck
Top achievements
Rank 5
Bronze
Bronze
Bronze
Answers by
Dilyan Traykov
Telerik team
Share this question
or