WPF DataGridTemplateColumn能见度MVVM下绑定 [英] WPF DataGridTemplateColumn Visibility Binding under MVVM
问题描述
我必须绑定到我的视图模型的ICollectionView一个DataGrid。 DataGrid的是这是在几个不同的数据方案,其中一些需要一定的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属性绑定到内层标签的内容属性,因此,如果没有行包含一个值,它会被隐藏。我有一个字符串能见度器集,但无法弄清楚如何找到内标贴的内容属性。
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的。为了解决这个问题,我不得不用code类同这样的:
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>
这篇关于WPF DataGridTemplateColumn能见度MVVM下绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!