MVVM 下的 WPF DataGridTemplateColumn 可见性绑定 [英] WPF DataGridTemplateColumn Visibility Binding under MVVM

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

问题描述

我有一个 DataGrid 绑定到我的 ViewModel 中的 ICollectionView.DataGrid 位于 UserControl 内,它用于几种不同的数据场景,其中一些需要某些 DataGrid 列,而另一些则不需要.

I have a DataGrid bound to an ICollectionView in my ViewModel. The DataGrid is inside a UserControl which is used in a few different data scenarios, some of which require certain DataGrid columns while others don't.

我只想将 DataGridTemplateColumn 的 Visibility 属性绑定到内部标签的 Content 属性,这样如果没有任何行包含值,它将被隐藏.我有一个 String to Visibility 转换器集,但不知道如何找到内部标签的 Content 属性.

I just want to bind the DataGridTemplateColumn's Visibility property to the inner label's Content property so if none of the rows contain a value, it will be hidden. I have a String to Visibility converter set, but can't figure out how to find the inner lable's Content property.

<DataGridTemplateColumn Header="Groups" Width="*" CanUserSort="True" SortMemberPath="Groups" Visibility="{Binding ElementName=lbl, Path=Content, Converter={StaticResource StringToVisibilityConverter}}">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Label Name="lbl" Content="{Binding Path=Groups}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

有什么建议吗?

推荐答案

我在 Stack Overflow 上的某个地方(找不到确切的帖子)读到 DataGridColumn 没有分配数据上下文,因为它们不是 FrameworkElement.为了解决这个问题,我不得不使用与此类似的代码:

I read somewhere on Stack Overflow(can't find exact post) that the DataGridColumn's aren't assigned a data context because they aren't a FrameworkElement. To get around this, I had to use code similiar to this:

    <DataGridTemplateColumn 
         Header="Groups" 
         Width="*" 
         CanUserSort="True" 
         SortMemberPath="Groups" 
         Visibility"{Binding RelativeSource={x:Static RelativeSource.Self}, 
                        Path=(FrameworkElement.DataContext).IsGroupsVisible, 
                        Converter={StaticResource booleanToVisiblityConverter}}">
         <DataGridTemplateColumn.CellTemplate>         
              <DataTemplate>             
                   <Label Name="lbl" Content="{Binding Path=Groups}" />         
              </DataTemplate>     
         </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 

Where 

<UserControl.Resources>
    <BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter" /> 
</UserControl.Resources>

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆