Search code examples
wpfbindingwpfdatagrid

WPF Hide DataGridColumn via a binding


For some reason I can't hide WPF Toolkit's DataGridColumn. I am trying to do the following:

<dg:DataGridTemplateColumn Header="Item Description" Visibility="{Binding IsReadOnly}">
<dg:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <TextBox Text="{Binding Path=ItemDescription}" />
    </DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>

This doesn't work, since it's looking for a IsReadOnly property on the ItemSource (not a property of the current class). If add this as a property of the ItemSource class that implements INoifyPropertyChanged, it still doesn't hide the column. Is there a way around this? I want the column to hid when a button click changes IsReadOnly property.

Assume IsReadOnly returns a Visibility value and is a dependency property

I am completely stuck, I would really appreciate the help! Thanks a lot!


Solution

  • If you want to bind to the DataGridColumn's IsReadOnly property, just add a RelativeSource to the Binding (and a converter):

    <BooleanToVisibilityConverter x:Key="boolToVis" />
    
    ...
    
    <dg:DataGridTemplateColumn Header="Item Description" Visibility="{Binding IsReadOnly, RelativeSource={RelativeSource Self}, Converter={StaticResource boolToVis}}">
    

    Also, it looks like this StackOverflow question might be related to your problem.