Articles in this section
Category / Section

How to bind the GridColumn.IsHidden property in UWP DataGrid ViewModel?

2 mins read

The column definitions of the DataGrid are not part of the SfDataGrid visual tree, so you can not bind SfDataGrid DataContext with GridColumn.

You can overcome this problem by defining ViewModel (DataContext) inside Resources and you can bind ViewModel to GridColumns by using StaticResource binding as in the following code example.

XAML

    <Window.Resources>
        <local: ViewModel x:Key="viewModel"/>
    </Window.Resources>
        <syncfusion:SfDataGrid x:Name="datagrid"
                               AllowSorting="True"
                               AutoGenerateColumns="False"
                               ColumnSizer="Star"
                               ItemsSource="{Binding GDCSource}" >
            <syncfusion:SfDataGrid.Columns>
                <syncfusion:GridTextColumn 
                                      IsHidden="{Binding Hide,                 
                                      Source={StaticResource viewModel}}" 
                                      MappingName="EmployeeDesignation" />
                <syncfusion:GridTextColumn MappingName="EmployeeArea"/>
                <syncfusion:GridTextColumn MappingName="EmployeeName"/>
                <syncfusion:GridTextColumn MappingName="EmployeeSalary"/>
                <syncfusion:GridDateTimeColumn MappingName="EmployeeDate"  />
                <syncfusion:GridTextColumn MappingName="EmployeeGender"/>
            </syncfusion:SfDataGrid.Columns>
        </syncfusion:SfDataGrid>

 

Here ViewModel is given as Windows Resources to access it as StaticResource for GridColumn Properties.

Another method is by using FindAncestor. In this method, ViewModel is defined as DataContext for MainWindow. You can bind MainWindow DataContext properties to GridColumns using RelativeResource. This FindAncestor is supported in WPF only.

Refer the following code example.


    <Window.DataContext>
        <local:ViewModel />
    </Window.DataContext>
    <Window.Resources>
        <local:ViewModel x:Key="viewModel"/>
    </Window.Resources>
        <syncfusion:SfDataGrid x:Name="datagrid"
                               AllowSorting="True"
                               AutoGenerateColumns="False"
                               ColumnSizer="Star"
                               ItemsSource="{Binding GDCSource}" >
            <syncfusion:SfDataGrid.Columns>
                <syncfusion:GridTextColumn 
                    IsHidden="{Binding Hide, 
                    Source={StaticResource viewModel}}" 
                    MappingName="EmployeeDesignation" />
            <syncfusion:GridTextColumn IsHidden="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MainWindow}},Path=DataContext.Hide}" MappingName="EmployeeArea"/>
                <syncfusion:GridTextColumn MappingName="EmployeeName"/>
                <syncfusion:GridTextColumn MappingName="EmployeeSalary"/>
                <syncfusion:GridDateTimeColumn MappingName="EmployeeDate"  />
                <syncfusion:GridTextColumn MappingName="EmployeeGender"/>
            </syncfusion:SfDataGrid.Columns>
        </syncfusion:SfDataGrid>
</Window>                       

 

Here Hide property is defined in ViewModel. So you can set ancestor type as MainWindow and path as DataContext.Hide. IsHidden property value is derived from ViewModel through MainWindow DataContext.

Sample Links:

WPF

UWP

 

Conclusion

I hope you enjoyed learning about how to bind the GridColumn.IsHidden property in UWP DataGrid ViewModel.

You can refer to our UWP DataGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our UWP DataGrid documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied