我有一个WPF应用程序,该应用程序的页面带有树视图,其树视图项目具有包含不同种类UI元素的多列。我不想为每个treeview项中的每个元素都提供工具提示,而是想为每个列都提供通用的工具提示。这将防止用户将鼠标移到树状视图中的所有元素上时始终弹出工具提示。可以在此处查看应用程序页面的示例。
Sample screenshot 我尝试了多种方法来执行此操作,并且正在寻找任何人的好主意。目前,我已经在树视图边框的顶部添加了透明边框,并在其上附加了工具提示。这是行不通的,因为透明元素不会捕获点击测试以启用工具提示。但是,如果我使边框背景不是透明的,边框将捕获命中测试,显示工具提示,并且基础控件将不起作用。
我知道这里的许多帖子都回答“为什么要这么做?”出于礼貌,我很欣赏实现目标的想法,而不是为什么我不尝试的理由。
我要显示的xaml具有可重用的自定义工具提示用户控件,该控件可让我提供一个密钥,该密钥可找到工具提示中用于相关元素的语言,文本和图像。效果很好,这不是此问题的一部分。谢谢大家。
<!--Tree view border-->
<Border x:Name="tnk_TreeBorder" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Grid.Column="0" Grid.Row="0" Margin="22,-2,26,2"
CornerRadius="6" BorderThickness="1,1,1,1"
BorderBrush="{DynamicResource MyBorderBrush}"
Background="{DynamicResource MyPopupBackgroundBrush}">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="280" />
<ColumnDefinition Width="326" />
<ColumnDefinition Width="110" />
<ColumnDefinition Width="46" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ctls:MyTreeView x:Name="tnk_TreeView" Style="{StaticResource MyTreeViewStyle}"
Grid.ColumnSpan="5"
ItemsSource="{Binding TreeNodes}"
HighlightSelected="False" Margin="2,2,2,2"
Background="Transparent" HorizontalAlignment="Stretch"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling">
<ctls:MyTreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource MyTreeViewItemStyle}">
<EventSetter Event="MouseEnter" Handler="TreeViewItem_MouseEnter" />
<EventSetter Event="MouseLeave" Handler="TreeViewItem_MouseLeave" />
<EventSetter Event="MouseDown" Handler="TreeViewItem_MouseDown" />
<EventSetter Event="Expanded" Handler="TreeViewItem_Expanded" />
</Style>
</ctls:MyTreeView.ItemContainerStyle>
<TreeView.ItemTemplate >
<HierarchicalDataTemplate ItemsSource="{Binding Children}" >
<Grid Height="45">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="260" />
<ColumnDefinition Width="300" />
<ColumnDefinition Width="120" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding ItemName}" Margin="6,6,0,0"
Foreground="{DynamicResource MyTextBrush}"/>
<views:GaugeView Grid.Column="1" Margin="2,0,0,10"
LeftText="{Binding MinText}" RightText="{Binding MaxText}"
Percent="{Binding Percent}" Color="{Binding Color}"
Orientation="LeftToRight"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
<Grid Grid.Column="2" Height="26" Margin="20,0,0,0">
<Border Height="26" Margin="0,0,0,0"
HorizontalAlignment="Stretch" VerticalAlignment="Center"
BorderBrush="{DynamicResource MyBorderBrush}" BorderThickness="1"
CornerRadius="4" Background="{DynamicResource MyDataLabelBackgroundBrush}" >
<DockPanel Grid.Column="0" HorizontalAlignment="Right" Margin="0,0,6,0">
<TextBlock DockPanel.Dock="Left" Text="{Binding DisplayValue}" TextAlignment="Right"
Foreground="{DynamicResource MyTextBrush}" Margin="2,-2,2,0" />
<TextBlock DockPanel.Dock="Right" Text="{Binding UoM}" FontSize="14" TextAlignment="Left"
Foreground="GhostWhite" Margin="2,4,2,0" />
</DockPanel>
</Border>
<ctls:MyBasicButton ButtonName="tnk.btn_Value"
Style="{StaticResource MyInvisibleButtonStyle}" Margin="0,0,0,0"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
CollectionKey="{Binding Key}"
Command="{Binding ButtonPressedCommand, Source={StaticResource viewmodelbase}}"
CommandParameter="{Binding Mode=OneWay, RelativeSource={RelativeSource Self}}"/>
</Grid>
<StackPanel Orientation="Horizontal" Grid.Column="3">
<ctls:MyBasicButton ButtonName="tnk.btn_Dampen"
Style="{StaticResource MyImageOnlyButtonStyle}"
Margin="10,0,0,0" Background="Transparent" BorderThickness="0"
CollectionKey="{Binding Key}"
Image="{Binding DampenImage, Converter={StaticResource StringToImageConverter}}"
MaxImageSize="20" HorizontalAlignment="Left"
Command="{Binding ButtonPressedCommand, Source={StaticResource viewmodelbase}}"
CommandParameter="{Binding Mode=OneWay, RelativeSource={RelativeSource Self}}"/>
<ctls:MyBasicButton ButtonName="tnk.btn_Valve" Style="{StaticResource MyInvisibleButtonStyle}"
CollectionKey="{Binding Key}" Margin="10,-2,0,0" Width="38" Height="25"
HorizontalAlignment="Center" VerticalAlignment="Center"
Visibility="{Binding ValveIsVisible, Converter={StaticResource BoolToVisibilityConverter}}"
Command="{Binding ButtonPressedCommand, Source={StaticResource viewmodelbase}}"
CommandParameter="{Binding Mode=OneWay, RelativeSource={RelativeSource Self}}">
<ContentControl Style="{Binding ValveNumber, Converter={StaticResource IntToValveConverter}}" />
</ctls:MyBasicButton>
<ctls:MyBasicButton ButtonName="tnk.btn_Transfer" Style="{StaticResource MyLEDButtonStyle}"
BorderBrush="{DynamicResource MyBorderBrush}"
CollectionKey="{Binding Key}" Margin="38,0,0,0"
Background="{Binding LEDBrushNumber, Converter={StaticResource IntToLEDBrushConverter}}"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Visibility="{Binding LEDIsVisible, Converter={StaticResource BoolToVisibilityConverter}}"
Command="{Binding ButtonPressedCommand, Source={StaticResource viewmodelbase}}"
CommandParameter="{Binding Mode=OneWay, RelativeSource={RelativeSource Self}}"/>
</StackPanel>
</Grid>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</ctls:MyTreeView>
<!--Borders for tooltips-->
<Border Grid.Column="0"
ToolTipService.InitialShowDelay="2000"
ToolTipService.BetweenShowDelay="0"
ToolTipService.ShowDuration="60000"
ToolTipService.ShowOnDisabled="True">
<Border.ToolTip >
<ctls:MyHelpTip Style="{StaticResource MyHelpTipStyle}"
Visibility="{Binding ShowHelpTips, Converter={StaticResource BoolToVisibilityConverter}}">
<views:HelpTipView DataContext="{StaticResource HelpTipViewModel}" ControlID="tnk_Names" />
</ctls:MyHelpTip>
</Border.ToolTip>
<Rectangle Fill="Transparent"/>
</Border>
<Border Grid.Column="1" Background="Red" Opacity=".2"
ToolTipService.InitialShowDelay="2000"
ToolTipService.BetweenShowDelay="0"
ToolTipService.ShowDuration="60000"
ToolTipService.ShowOnDisabled="True">
<Border.ToolTip >
<ctls:MyHelpTip Style="{StaticResource MyHelpTipStyle}"
Visibility="{Binding ShowHelpTips, Converter={StaticResource BoolToVisibilityConverter}}">
<views:HelpTipView DataContext="{StaticResource HelpTipViewModel}" ControlID="tnk_Gauges" />
</ctls:MyHelpTip>
</Border.ToolTip>
<Rectangle Fill="Transparent" />
</Border>
<!--Background colors here are for screenshot only-->
<Border Grid.Column="2" Background="PaleVioletRed" Opacity=".2">
<Rectangle Fill="Transparent" />
</Border>
<Border Grid.Column="3" Background="CornflowerBlue" Opacity=".2">
<Rectangle Fill="Transparent" />
</Border>
<Rectangle Fill="Transparent" />
<Border Grid.Column="4" Margin="0,0,20,0" Background="SeaGreen" Opacity=".2">
</Border>
</Grid>
</Border>