How to bind the GridColumn property in ViewModel in WPF DataGRid?
The column definitions of the DataGrid are not part of the WPF DataGrid 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:
Conclusion
I hope you enjoyed learning about how to bind the GridColumn.IsHidden property to property in ViewModel in WPF 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!