我有一个带有层次数据的树视图控件。它有一个带有四个选项的上下文菜单:Expand、Expand All、Collapse、Collapse All。我目前正在使用下列类别来显示/隐藏内容功能表项目:
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,反之亦然。同样,如果它是一个根父级节点,我应该看到Expand All。那么,我必须为所有四种情况编写单独的转换器吗?或者有智能的方法可以做到这一点吗?
如果需要更多信息,请告诉我。
1条答案
按热度按时间wyyhbhjk1#
您可能需要两个转换器或两对转换器。一个用于扩展状态,另一个用于返回节点是父节点还是子节点。如果只使用两个转换器,则需要一个参数来确定转换器应返回
Visibility.Visible
还是Visibility.Collapsed
。如果有四个转换器,则不需要参数。