views:

333

answers:

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!

A: 

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

ecathell
Assume IsReadOnly already returns a visibility value, that's not my problem, my problem is that data grid does not want to respect it
Greg R
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.
ecathell
The BooleanToVisibilityConverter class already exists in System.Windows.Controls, no need to recreate it ;)
Thomas Levesque
A: 

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.

Abe Heidebrecht
Excellent, worked like a charm, thank you very much!
Greg R
+1  A: 

Posted in this question http://bit.ly/bSD1Dq 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 http://bit.ly/2klIFN, 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?

Ed Gonzalez
Thank you very much, this is very similar to the answer below.I did see binding errors, so I knew I had problems :(
Greg R
Glad it's up and working now.
Ed Gonzalez