Bind DataGridTextColumn Visibility Property in WPF
Asked Answered
A

1

5

I have a datagrid whose ItemsSource binds to a CollectionViewSource.
In each column I specify the Path property of the binding to get the specific information to display.

What I'd like to do is toggle some of the columns with a checkbox if the user wants more info. To do this, I need to bind the visibility property to the value of the checkbox (with a converter) but I'm pretty sure the data context of the column is interfering with the binding.

<DataGrid ItemsSource="{Binding Source={StaticResource cvs}}" ....>
    <DataGrid.Columns>
        <DataGridTextColumn Header="First Name" Binding="{Binding Path=FirstName}"
            Visibility="{Binding IsChecked,
                                 ElementName=IncludeFullHist, 
                                 Converter={StaticResource boolItemsConverter}}"/>   
    </DataGrid.Columns>
</DataGrid>

I need the checkbox in my viewmodel as well, so I have its IsChecked property bound to a property on my ViewModel

<CheckBox x:Name="IncludeFullHist"  IsChecked="{Binding Path=ManagerFullHist }" />

For other elements in my page, I've been able to hook up visibility bindings with either of the two following methods, but neither seem to work when I copy them over into the datagrid:

<TextBlock DockPanel.Dock="Left" Text=" Visible 2 " 
    Visibility="{Binding Path=DataContext.ManagerFullHist,
                         RelativeSource={RelativeSource FindAncestor,
                         AncestorType={x:Type UserControl}},
                         Converter={StaticResource boolItemsConverter}}"/>
<TextBlock DockPanel.Dock="Left" Text=" Visible 3 " 
    Visibility="{Binding Path=ManagerFullHist, 
                         Source={StaticResource mainWinResource},
                         Converter={StaticResource boolItemsConverter}}"/>

Any suggestions on ways that I can solve this in the datagrid?
Please let me know if I've omitted any code that could be potentially helpful.

Aniakudo answered 27/8, 2012 at 15:1 Comment(3)
@Rachel Rachel, Thanks for your answer! This is the exact problem I'm having. I feel much closer, but the binding still isn't working. I set up the BindingProxy class as per the linked article. The DataContext for my user control is set to my ViewModel class and my datagrid inherits its datacontext from that. I've tried putting the "proxy" resource in my datagrid and my user control and tried binding to the Path=DataContext.ManagerFullHist and to the IsChecked property of ElementName=IncludeFullHist and none of those work. Is the datacontext for my control messing things up?Aniakudo
@Rachel, Again, thanks very much for your answer. I got it working. The problem was something silly on my end where I was trying to get ahold of the DataContext in one of my other workarounds by giving it an x:Key attribute (<vm:MainWindowViewModel x:Key="MWVM"/>) which was interfering somehow with the datacontext. Either way, the solution works as posted. Thanks!Aniakudo
Glad you got it worked out :)Uncut
U
11

The DataGridColumn is not actually part of the VisualTree, so bindings on the class cannot find their source

You can set things like the Visibility and Width property in the CellStyle or HeaderStyle of the DataGridColumn, although that isn't quite the same.

The closest I've found to a solution would be to create a Freezable object in your <DataGrid.Resources> that stores the binding, then use that StaticResource in the Visibility binding. It's not a pretty solution, but it's the only one I can find at this time.

You can view of an example of it here

<DataGrid.Resources>
    <local:BindingProxy x:Key="proxy" Data="{Binding IsChecked, 
         ElementName=IncludeFullHist, 
         Converter={StaticResource boolItemsConverter}}" />
</DataGrid.Resources>

<DataGridTextColumn Header="First Name" Binding="{Binding Path=FirstName}"
    Visibility="{Binding Path=Data, Source={StaticResource proxy}}"/>  
Uncut answered 27/8, 2012 at 16:38 Comment(1)
Perhaps this was necessary in 2012, but it's worth noting that there's a much simpler solution.Illaudable

© 2022 - 2024 — McMap. All rights reserved.