绑定WPF中的DataGridTextColumn可见性属性

绑定WPF中的DataGridTextColumn可见性属性

本文介绍了绑定WPF中的DataGridTextColumn可见性属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagrid,它的 ItemsSource 绑定到一个 CollectionViewSource

在每列中我指定绑定的路径属性以获取要显示的特定信息。

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.

我想要如果用户想要更多的信息,则使用复选框来切换一些列。为此,我需要将visibility属性绑定到复选框的值(使用转换器),但我很确定列的数据上下文干扰绑定。

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>

我还需要我的viewmodel中的复选框,所以我的 IsChecked 属性绑定到我的ViewModel上的一个属性

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 }" />






对于页面中的其他元素,已经能够使用以下两种方法来连接可见性绑定,但是当我将它们复制到datagrid中时似乎不起作用:


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}}"/>

有什么建议可以在datagrid中解决这个问题?

请让我知道如果我省略了可能有帮助的任何代码。

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.

推荐答案

DataGridColumn 实际上并不属于 VisualTree 的一部分,所以类上的绑定找不到源代码

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

您可以在 CellStyle Visibility Width 属性$ c>或 DataGridColumn 的HeaderStyle ,尽管不一样。

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.

我找到的解决方案最接近的是创建一个对象,然后使用 StaticResource 可见性中绑定。这不是一个漂亮的解决方案,但它是目前唯一可以找到的。

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.

您可以查看一个例子

<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}}"/>

这篇关于绑定WPF中的DataGridTextColumn可见性属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:12