WPF Hide DataGridColumn via a binding

不打扰是莪最后的温柔 提交于 2019-12-21 20:29:10

问题


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!


回答1:


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.




回答2:


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?




回答3:


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



来源:https://stackoverflow.com/questions/2518484/wpf-hide-datagridcolumn-via-a-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!