【发布时间】:2016-11-21 14:20:54
【问题描述】:
我有一个带有分层数据的树视图控件。它有一个包含四个选项的上下文菜单:展开、全部展开、折叠、全部折叠。我目前正在使用以下类来显示/隐藏上下文菜单项:
Public Class clsTreeContextMenuVisibilityConverter
Implements IValueConverter
Public Function Convert(InValue As Object, InTargetType As Type, InParameter As Object, InCulture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
Dim node As TreeNode = Nothing
If InValue Is Nothing Then
Return Binding.DoNothing
End If
node = DirectCast(InValue, TreeNode)
If InValue.[GetType]() <> GetType([Boolean]) Then
If node.HasChildren AndAlso node.ParentNode Is Nothing Then
If node.IsExpanded Then
Return Visibility.Collapsed
End If
Return Visibility.Visible
End If
End If
Return Binding.DoNothing
End Function
Public Function ConvertBack(InValue As Object, InTargetType As Type, InParameter As Object, InCulture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
结束类
XAML:
<Style x:Key="ExpandMenuItemStyle"
TargetType="MenuItem">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Visibility" Value="{Binding Converter={StaticResource VisConverter}}" />
</Style>
<MenuItem Header="Expand" Style="{StaticResource ExpandMenuItemStyle}" />
VisConverter 是转换器类的 x:Key。我的问题是,如果节点展开,我应该看到 Collapse,反之亦然。此外,如果它是根父级节点,那么我应该看到全部展开。那么我是否必须为所有四种情况编写单独的转换器,还是有一种智能的方法可以做到这一点?
如果需要更多信息,请告诉我。
【问题讨论】:
标签: wpf contextmenu collapse expand