WPF Hide DataGridColumn via a binding
Asked Answered
H

3

3

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!

Hyderabad answered 25/3, 2010 at 18:56 Comment(0)
C
5

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.

Chlamydate answered 25/3, 2010 at 20:21 Comment(0)
B
6

Posted in this question:

WPF DataGrid: Binding DataGridColumn visibility to ContextMenu MenuItems Ischeked (MVVM)

Fubzot is using the binding code similar to

Visibility='{Binding (FrameworkElement.DataContext).IsReadOnly,
RelativeSource={x:Static RelativeSource.Self}}'

You may also want to check out this:

Forwarding the DataGrid’s DataContext to its’ columns..

which is also linked in the above question.

Just for my information: Do you see any Binding errors in your Output window using your current code?

Burtonburty answered 25/3, 2010 at 20:43 Comment(1)
Thank you very much, this is very similar to the answer below. I did see binding errors, so I knew I had problems :(Hyderabad
C
5

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.

Chlamydate answered 25/3, 2010 at 20:21 Comment(0)
A
-1

you need to use a converter

 Public Class BooleanToVisibilityConverter
        Implements IValueConverter
        Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            If targetType Is GetType(Visibility) Then
                If CBool(value) = True Then
                    Return Visibility.Hidden
                Else
                    Return Visibility.Visible
                End If
            Else
                Return Nothing
            End If
        End Function

        Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
            Return Nothing
        End Function
    End Class

then you use the converter in the XAML. SAMPLE

Aeroembolism answered 25/3, 2010 at 19:16 Comment(3)
Assume IsReadOnly already returns a visibility value, that's not my problem, my problem is that data grid does not want to respect itHyderabad
ok..silly question. On you VM property IsReadOnly do you have OnPropertyChanged("IsReadOnly") in your setter? I know sometimes I forget to do that and the UI wont react without it.Aeroembolism
The BooleanToVisibilityConverter class already exists in System.Windows.Controls, no need to recreate it ;)Chiffonier

© 2022 - 2024 — McMap. All rights reserved.