代码之家  ›  专栏  ›  技术社区  ›  Craig Vermeer

从Silverlight上下文菜单中删除鼠标悬停效果

  •  0
  • Craig Vermeer  · 技术社区  · 14 年前

    下面是我想做的一个片段:

    <toolkit:ContextMenuService.ContextMenu>
            <toolkit:ContextMenu Height="100" Background="Transparent" HorizontalOffset="-100" VerticalOffset="-100" Margin="98,112,0,0" Name="contextMenu1" VerticalAlignment="Top" Width="200">
                <toolkit:ContextMenu.Style>
                    <Style TargetType="toolkit:ContextMenu">
                        <Setter Property="BorderThickness" Value="0" />
                        <Setter Property="Padding" Value="0" />
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="toolkit:ContextMenu">
                                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="2">
                                        <Grid>
                                            <ItemsPresenter Margin="{TemplateBinding Padding}" />
                                        </Grid>
                                    </Border>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </toolkit:ContextMenu.Style>
                <Canvas Width="100" Height="100" Background="Transparent">
                    <Button Width="100" Height="30">Something</Button>
                    <Button Width="100" Height="30" Canvas.Top="70">Something Else</Button>
                </Canvas>
            </toolkit:ContextMenu>
        </toolkit:ContextMenuService.ContextMenu>
    

    当菜单可见时,我得到了我想要的效果(两个按钮只是在鼠标附近浮动),但是当我将鼠标移到它上面时,上下文菜单的整个框会高亮显示。

    下面的示例应用程序演示了这一点:

    http://github.com/vermeeca/ContextMenuDemo

    我怎样才能禁用这种效果?

    1 回复  |  直到 14 年前
        1
  •  1
  •   Craig Vermeer    14 年前

    <UserControl.Resources>
        <Style x:Key="NoMouseOverStyle" TargetType="toolkit:MenuItem">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="BorderBrush" Value="Transparent"/>
            <Setter Property="Padding" Value="4,3,2,3"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="toolkit:MenuItem">
                        <Grid>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0" To="0.5" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Presenter"/>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="FocusStates">
                                    <VisualState x:Name="Unfocused"/>
                                    <!-- VisualState x:Name="Focused">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Bg"/>
                                            <ColorAnimation Duration="0" To="#40FFFFFF" Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)" Storyboard.TargetName="InnerBorder"/>
                                        </Storyboard>
                                    </VisualState -->
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Rectangle Fill="{TemplateBinding Background}" RadiusY="2" RadiusX="2" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="1"/>
                            <Rectangle x:Name="Bg" Opacity="0" RadiusY="2" RadiusX="2" Stroke="#8071CBF1" StrokeThickness="1">
                                <Rectangle.Fill>
                                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                        <GradientStop Color="#34C5EBFF" Offset="0"/>
                                        <GradientStop Color="#3481D8FF" Offset="1"/>
                                    </LinearGradientBrush>
                                </Rectangle.Fill>
                            </Rectangle>
                            <Rectangle x:Name="InnerBorder" Margin="1" RadiusY="2" RadiusX="2" Stroke="Transparent"/>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition MinWidth="24" Width="Auto"/>
                                    <ColumnDefinition Width="4"/>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="17"/>
                                </Grid.ColumnDefinitions>
                                <ContentPresenter Content="{TemplateBinding Icon}" Margin="1" VerticalAlignment="Center"/>
                                <ContentPresenter x:Name="Presenter" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Grid.Column="2" Margin="{TemplateBinding Padding}"/>
                            </Grid>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    

    然后

    <toolkit:ContextMenu Height="100" Background="Transparent" HorizontalOffset="-100" VerticalOffset="-100" Margin="98,112,0,0" Name="contextMenu1" VerticalAlignment="Top" Width="200" ItemContainerStyle="{StaticResource NoMouseOverStyle}">