I am using the DataGrid control from Windows Community Toolkit for UWP, but I can't find a solution to dynamically hide or show columns.
Runtime error: Failed to assign to property 'Microsoft.Toolkit.Uwp.Ui.Controls.DataGridColumn.Visibility'
<controls:DataGrid x:Name="TradeGrid">
<controls:DataGridTextColumn
x:Uid="DataColumnPositionContract"
Width="SizeToCells"
Binding="{Binding Path=TradePosition.Contract.Name}"
Tag="Contract"
Visibility="{Binding ElementName=TradeGrid, Path=DataContext.IsContractVisible, Converter={StaticResource BoolToVisConv}}" />
</controls:DataGrid>
The view model structure looks like:
PageViewModel (bound to page)
- List of items (itemsource of DataGrid)
- Column visibility properties
ItemViewModel (DataContext of a column)
- Item properties
The converter is the converter provided by the toolkit. The boolean property to bind is not part of the DataContext of the column (ItemViewModel). I need to access the parent view model (PageViewModel) to get the property.
Can anyone give me a hint on how to bind to the Visibility
property of a DataGridColumn?
I am open for other solutions if the binding is not possible this way in UWP.
Thanks!