MenuItem没有显示工具提示

时间:2016-09-02 07:26:15

标签: wpf custom-controls menuitem

我为自定义Control(ButtonAnalysisControl)创建了一个样式。除了ToolTip之外,一切正常。当我将鼠标移到MenuItem上时,ToolTip无法显示。我检查了MenuItems Snoop并且ToolTip值已正确设置。如何使ToolTip工作?加分问题:如何删除Border中的ContextMenu

文本菜单:

ContextMenu

Generic.xaml Style

<Style TargetType="anal:ButtonAnalysisControl">

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="anal:ButtonAnalysisControl">

                <ControlTemplate.Triggers>
                    <EventTrigger RoutedEvent="Click">
                        <BeginStoryboard>
                            <Storyboard>
                                <BooleanAnimationUsingKeyFrames Storyboard.TargetName="ContextMenu" Storyboard.TargetProperty="(ContextMenu.IsOpen)">
                                    <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="True"/>
                                </BooleanAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </ControlTemplate.Triggers>

                <ControlTemplate.Resources>
                    <anal:CustomMultiValueConvertor x:Key="CustomMultiValueConvertor"/>
                    <anal:IntToPercentageBrushConvertor x:Key="IntToPercentageBrushConvertor"/>
                </ControlTemplate.Resources>

                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <Grid.ContextMenu>
                        <ContextMenu Name="ContextMenu" IsOpen="False" Placement="Top" PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}">
                            <ContextMenu.ItemsSource>
                                <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="ChildCommands"/>
                            </ContextMenu.ItemsSource>
                            <ContextMenu.ItemContainerStyle>
                                <Style TargetType="{x:Type MenuItem}">
                                    <Setter Property="MenuItem.Header">
                                        <Setter.Value>
                                            <MultiBinding Converter="{StaticResource CustomMultiValueConvertor}">
                                                <Binding Path="Percentage" />
                                                <Binding Path="ViewCommand.Command.Text" />
                                            </MultiBinding>
                                        </Setter.Value>
                                    </Setter>
                                    <Setter Property="MenuItem.IsEnabled" Value="False"/>
                                    <Setter Property="MenuItem.Background" Value="{Binding Percentage, Converter={StaticResource IntToPercentageBrushConvertor}}"/>
                                    <Setter Property="MenuItem.Padding" Value="0"/>
                                    <Setter Property="MenuItem.BorderThickness" Value="0"/>    
                                    <Setter Property="MenuItem.ToolTip" Value="123"/>                                        
                                </Style>
                            </ContextMenu.ItemContainerStyle>
                            <ContextMenu.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <VirtualizingStackPanel Orientation="Vertical"/>
                                </ItemsPanelTemplate>
                            </ContextMenu.ItemsPanel>
                        </ContextMenu>
                    </Grid.ContextMenu>

                    <TextBlock TextAlignment="Center"
                               VerticalAlignment="Stretch" 
                               Foreground="{StaticResource CommandBarForeground}" 
                               Background="{StaticResource MainForegroundBrush}"
                               FontFamily="{StaticResource FontFamily}"
                               FontSize="10"
                               Grid.Column="0" 
                               Grid.Row="0">
                        <TextBlock.Text>
                            <Binding Path="Text" StringFormat="{}{0}%" RelativeSource="{RelativeSource TemplatedParent}" />
                        </TextBlock.Text>                            
                    </TextBlock>
                    <Rectangle Grid.Column="0" 
                               Grid.Row="1">
                        <Rectangle.Fill>
                            <Binding Path="BackgroundBrush" RelativeSource="{RelativeSource TemplatedParent}" />
                        </Rectangle.Fill>
                    </Rectangle>                        
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

</Style>

1 个答案:

答案 0 :(得分:1)

我找到了答案:ToolTip没有显示,因为MenuItem已被禁用。

将此行添加到我的MenuItem样式中修复它:

<Setter Property="ToolTipService.ShowOnDisabled" Value="True" />