从Silverlight上下文菜单中删除MouseOver效果

时间:2010-08-11 00:34:26

标签: silverlight silverlight-4.0 silverlight-toolkit

当用户右键单击显示自定义控件的Silverlight应用程序时,我正在尝试实现弹出窗口的效果。我正在使用上下文菜单,除了我在设置上下文菜单时遇到问题,以便当用户将鼠标悬停时不突出显示,所有内容都很有效。

以下是我正在尝试做的事情的片段:

<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 个答案:

答案 0 :(得分:1)

感谢同事,我得到了解决方案。我只需要在ContextMenu上设置ItemContainerStyle。

<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}">