WPF 通过绑定隐藏 DataGridColumn

发布于 2024-08-26 12:55:07 字数 637 浏览 6 评论 0原文

由于某种原因,我无法隐藏 WPF Toolkit 的 DataGridColumn。我正在尝试执行以下操作:

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

这不起作用,因为它正在寻找 ItemSource 上的 IsReadOnly 属性(而不是当前类的属性)。 如果将此添加为实现 INoifyPropertyChanged 的​​ ItemSource 类的属性,它仍然不会隐藏该列。有办法解决这个问题吗?我希望当单击按钮更改 IsReadOnly 属性时隐藏该列。

假设 IsReadOnly 返回一个可见性值并且是一个依赖属性

我完全陷入困境,我真的很感谢您的帮助!多谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葵雨 2024-09-02 12:55:07

发表在这个问题中:

WPF DataGrid:将 DataGridColumn 可见性绑定到 ContextMenu MenuItems Ischeked (MVVM)

Fubzot 使用的绑定代码类似于

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

您可能还想查看一下:

将 DataGrid 的 DataContext 转发到其' columns..

这也链接在上面的问题中。

仅供我参考:您是否在使用当前代码的输出窗口中看到任何绑定错误?

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?

太阳公公是暖光 2024-09-02 12:55:07

如果您想绑定到 DataGridColumnIsReadOnly 属性,只需将 RelativeSource 添加到 Binding(和转换器)即可:

<BooleanToVisibilityConverter x:Key="boolToVis" />

...

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

另外,它看起来此 StackOverflow 问题可能与您的问题有关。

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.

桃扇骨 2024-09-02 12:55:07

您需要使用转换器

 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

,然后在 XAML 中使用转换器。 示例

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文