将鼠标悬停在WPF菜单中的菜单项之外的控件上

时间:2014-06-30 09:54:47

标签: wpf menu hover controls menuitem

我有一个带有菜单项的菜单,一个网格和一个按钮,我想要的是网格和按钮,当鼠标像其他菜单项一样悬停时不要突出显示。 我把你的xaml代码留在这里。

<Menu DockPanel.Dock="Top" Panel.ZIndex="1" x:Name="MainMenu" Height="25">
    <Menu.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="Black" Offset="0" />
            <GradientStop Color="#FF4C5568" Offset="1" />
        </LinearGradientBrush>
    </Menu.Background>
    <MenuItem Header="Medio" Foreground="White" HorizontalAlignment="Left">
        <MenuItem Header="Abrir archivo" x:Name="OpenVideoFile" Foreground="Black" />
        <MenuItem Header="Abrir disco" x:Name="OpenVideoDisc" Foreground="Black" />
    </MenuItem>
    <MenuItem Header="Reproducción" SubmenuOpened="MenuItem_SubmenuOpened" Foreground="White" HorizontalAlignment="Left">
        <MenuItem Header="Título" Foreground="Black" x:Name="TitleList" IsEnabled="{Binding IsDisc}" ItemsSource="{Binding TitleList}" ItemTemplate="{DynamicResource trackMenuItemTemplate}" />
        <MenuItem Header="Capítulo" Foreground="Black" x:Name="ChapterList" IsEnabled="{Binding IsDisc}" ItemsSource="{Binding ChapterList}" ItemTemplate="{DynamicResource trackMenuItemTemplate}" />
        <MenuItem Header="Subtítulo" Foreground="Black" x:Name="SubtitleList" IsEnabled="{Binding IsDisc}" ItemsSource="{Binding SubtitlesList}" ItemTemplate="{DynamicResource trackMenuItemTemplate}" />
        <MenuItem Header="Pista Vídeo" Foreground="Black" x:Name="VideoTrack" IsEnabled="{Binding IsDisc}" ItemsSource="{Binding VideoTracks}" ItemTemplate="{DynamicResource trackMenuItemTemplate}" />
        <MenuItem Header="Pista Audio" Foreground="Black" x:Name="AudioTrack" IsEnabled="{Binding IsDisc}" ItemsSource="{Binding AudioTracks}" ItemTemplate="{DynamicResource trackMenuItemTemplate}" />
    </MenuItem>
    <Grid Height="27" Width="452" MouseLeftButtonDown="WindowMouseLeftButtonDown" Visibility="Visible" Background="Transparent" />
    <Button Width="50" BorderThickness="0" x:Name="Close" HorizontalAlignment="Right" VerticalAlignment="Bottom" Style="{StaticResource CloseStyle}" Height="22" Click="Close_Click" />
</Menu>

提前致谢

1 个答案:

答案 0 :(得分:0)

按钮和网格位于菜单控件内部,这就是为什么菜单将Button和grid视为menuitem,并且它还使用Menu项默认样式作为按钮和网格。

使用menuitem 的MenuItem样式设计平面样式

Recource

 <Window.Resources>
    <Style x:Key="MenuitemStyle" TargetType="MenuItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="MenuItem">
                    <Grid>
                        <ContentPresenter Content="{TemplateBinding Header}"></ContentPresenter>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

的Xaml

<Menu DockPanel.Dock="Top" Panel.ZIndex="1" x:Name="MainMenu" Height="25">
    <Menu.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="Black" Offset="0"/>
            <GradientStop Color="#FF4C5568" Offset="1"/>
        </LinearGradientBrush>
    </Menu.Background>
    <MenuItem Header="Medio" Foreground="White" HorizontalAlignment="Left">
        <MenuItem Header="Abrir archivo" x:Name="OpenVideoFile" Foreground="Black"/>
        <MenuItem Header="Abrir disco" x:Name="OpenVideoDisc" Foreground="Black"/>
    </MenuItem>
    <MenuItem Header="Reproducción" Foreground="White" HorizontalAlignment="Left">
        <MenuItem Header="Título" Foreground="Black" x:Name="TitleList"   />
        <MenuItem Header="Capítulo" Foreground="Black" x:Name="ChapterList"/>
        <MenuItem Header="Subtítulo" Foreground="Black" x:Name="SubtitleLis"/>
        <MenuItem Header="Pista Vídeo" Foreground="Black" x:Name="VideoTrac"/>
        <MenuItem Header="Pista Audio" Foreground="Black" x:Name="AudioTrack"/>
    </MenuItem>
    <MenuItem Style="{StaticResource MenuitemStyle}" Width="452" Height="27"     >
        <MenuItem.Header>
            <Grid Visibility="Visible" MouseLeftButtonDown="WindowMouseLeftButtonDown" Background="Transparent"/>
        </MenuItem.Header>
    </MenuItem>
    <MenuItem Width="50" Height="22" Style="{StaticResource MenuitemStyle}">
        <MenuItem.Header>
            <Button Width="50" Height="22" Click="Close_Click" BorderThickness="0" HorizontalAlignment="Right" VerticalAlignment="Bottom" Foreground="Black" Content="Close" />
        </MenuItem.Header>
    </MenuItem>
</Menu>

使用Stackpanel

<Grid Name="Gd">
    <StackPanel Orientation="Horizontal" VerticalAlignment="Center" Width="{Binding ElementName=Gd,Path=ActualWidth}">
        <StackPanel.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="Black" Offset="0"/>
                <GradientStop Color="#FF4C5568" Offset="1"/>
            </LinearGradientBrush>
        </StackPanel.Background>
        <Menu DockPanel.Dock="Top" Panel.ZIndex="1" x:Name="MainMenu" Height="25">
            <Menu.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black" Offset="0"/>
                    <GradientStop Color="#FF4C5568" Offset="1"/>
                </LinearGradientBrush>
            </Menu.Background>
            <MenuItem Header="Medio" Foreground="White" HorizontalAlignment="Left">
                <MenuItem Header="Abrir archivo" x:Name="OpenVideoFile" Foreground="Black"/>
                <MenuItem Header="Abrir disco" x:Name="OpenVideoDisc" Foreground="Black"/>
            </MenuItem>
            <MenuItem Header="Reproducción" Foreground="White" HorizontalAlignment="Left">
                <MenuItem Header="Título" Foreground="Black" x:Name="TitleList"   />
                <MenuItem Header="Capítulo" Foreground="Black" x:Name="ChapterList"/>
                <MenuItem Header="Subtítulo" Foreground="Black" x:Name="SubtitleLis"/>
                <MenuItem Header="Pista Vídeo" Foreground="Black" x:Name="VideoTrac"/>
                <MenuItem Header="Pista Audio" Foreground="Black" x:Name="AudioTrack"/>
            </MenuItem>
        </Menu>
        <Grid Height="27" Width="452" Background="Transparent" Visibility="Visible"/>                        
        <Button Width="50" VerticalAlignment="Center" BorderThickness="0"  Content="Ok" Foreground="Black" x:Name="Close"/>
    </StackPanel>
</Grid>