Articles in this section
Category / Section

How to bind the GridColumn property in ViewModel in WPF DataGrid?

6 mins read

The column definitions of the WPF DataGrid (SfDataGrid) are not part of the DataGrid visual tree, so you can not bind DataGrid 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.

<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 MappingName="EmployeeDesignation"
                                   IsHidden="{Binding Hide, Source={StaticResource viewModel}}" />
        <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 MappingName="EmployeeDesignation"
                                   IsHidden="{Binding Hide, Source={StaticResource viewModel}}" />
        <syncfusion:GridTextColumn MappingName="EmployeeArea"
                                   IsHidden="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MainWindow}}, Path=DataContext.Hide}" />
        <syncfusion:GridTextColumn MappingName="EmployeeName" />
        <syncfusion:GridTextColumn MappingName="EmployeeSalary" />
        <syncfusion:GridDateTimeColumn MappingName="EmployeeDate" />
        <syncfusion:GridTextColumn MappingName="EmployeeGender" />
    </syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>

Here Hide property is defined in ViewModel. So you can set ancestor type as MainWindow and path as DataContext.HideIsHidden 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 to property in ViewModel in DataGrid.

You can refer to our WPF DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF DataGrid example 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 forums, Direct-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  to leave a comment
Access denied
Access denied