_wpf 右键菜单">
赞
踩
WPF中的右键菜单主要是通过ContextMenu来实现,ContextMenu的使用有两种方式
一、.在Resources中声明,在控件中使用。
1.在Resources中添加ContextMenu
- <Window.Resources>
- <ContextMenu x:Key="ContextMenu">
- <MenuItem Name="MiFullScreen" Header="全屏"/>
- <Separator></Separator>
- <MenuItem Name="MiPlay" Header="播放"/>
- </ContextMenu>
- </Window.Resources>
2.在控件中使用ContextMenu
定义在哪些地方使用右键,在需要使用此右键菜单的属性上添加:ContextMenu="{StaticResource ContextMenu}"
- <ListBox ContextMenu="{StaticResource ContextMenu}">
-
- </ListBox>
3.为每个MenuItem写Click事件
- <Window.Resources>
- <ContextMenu x:Key="ContextMenu">
- <MenuItem Name="NewFile" Click="NewFile_Click" Header="新建文件夹"/>
- <Separator></Separator>
- <MenuItem Name="Refresh" Click="Refresh_Click" Header="刷新"/>
- </ContextMenu>
- </Window.Resources>
次方法的优点在于只声明一次,可以在多个控件中同时使用
二、在控件中直接声明
- <ListBox >
- <ListBox.ContextMenu>
- <ContextMenu>
- <MenuItem Name="MenuItem1" Header="文件" Tag="{Binding Path=SelectdIndex" Click="MenuItem1_Click"/>
- <MenuItem Name="MenuItem2" Header="编辑" Tag="{Binding Path=SelectdIndex" Click="MenuItem1_Click"/>
- </ContextMenu>
- </ListBox.ContextMenu>
- </ListBox>
这些style包含了contexrMenu和MenuItem的内容,你可以改变其中的某项内容来达到你的要求。
- <!--自定义ContextMenu的外观样式-->
- <Style TargetType="{x:Type ContextMenu}">
- <Setter Property="SnapsToDevicePixels" Value="True"/>
- <Setter Property="OverridesDefaultStyle" Value="True"/>
- <Setter Property="Grid.IsSharedSizeScope" Value="True"/>
- <Setter Property ="Focusable" Value="False"/>
- <Setter Property="HasDropShadow" Value="True"/>
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type ContextMenu}">
- <Border
- Name="Border"
- Background="#6EA0B0"
- BorderBrush="#888888"
- BorderThickness="1" >
- <StackPanel IsItemsHost="True"
- KeyboardNavigation.DirectionalNavigation="Cycle"/>
- </Border>
- <ControlTemplate.Triggers>
- <Trigger Property="HasDropShadow" Value="true">
- <Setter TargetName="Border" Property="Padding" Value="0,3,0,3"/>
- <Setter TargetName="Border" Property="CornerRadius" Value="4"/>
- </Trigger>
- </ControlTemplate.Triggers>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
-
- <!--自定义菜单项之间的分隔符的样式-->
- <Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="{x:Type Separator}">
- <Setter Property="Height" Value="1"/>
- <Setter Property="Margin" Value="0,4,0,4"/>
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type Separator}">
- <Border BorderBrush="#888888" BorderThickness="1"/>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
-
- <!-- TopLevelHeader -->
- <ControlTemplate x:Key="{x:Static MenuItem.TopLevelHeaderTemplateKey}" TargetType="{x:Type MenuItem}">
- <Border Name="Border" >
- <Grid>
- <ContentPresenter
- Margin="6,3,6,3"
- ContentSource="Header"
- RecognizesAccessKey="True" />
- <Popup
- Name="Popup"
- Placement="Bottom"
- IsOpen="{TemplateBinding IsSubmenuOpen}"
- AllowsTransparency="True"
- Focusable="False"
- PopupAnimation="Slide">
- <Border
- Name="SubmenuBorder"
- SnapsToDevicePixels="True"
- Background="#FFFFFF"
- BorderBrush="#888888"
- BorderThickness="1" >
- <StackPanel
- IsItemsHost="True"
- KeyboardNavigation.DirectionalNavigation="Cycle" />
- </Border>
- </Popup>
- </Grid>
- </Border>
- <ControlTemplate.Triggers>
- <Trigger Property="IsSuspendingPopupAnimation" Value="true">
- <Setter TargetName="Popup" Property="PopupAnimation" Value="None"/>
- </Trigger>
- <Trigger Property="IsHighlighted" Value="true">
- <Setter TargetName="Border" Property="Background" Value="Green"/>
- <Setter TargetName="Border" Property="BorderBrush" Value="Transparent"/>
- </Trigger>
- <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="True">
- <Setter TargetName="SubmenuBorder" Property="CornerRadius" Value="0,0,4,4"/>
- <Setter TargetName="SubmenuBorder" Property="Padding" Value="0,0,0,3"/>
- </Trigger>
- <Trigger Property="IsEnabled" Value="False">
- <Setter Property="Foreground" Value="#888888"/>
- </Trigger>
- </ControlTemplate.Triggers>
- </ControlTemplate>
-
- <!-- TopLevelItem -->
- <ControlTemplate
- x:Key="{x:Static MenuItem.TopLevelItemTemplateKey}"
- TargetType="{x:Type MenuItem}">
- <Border Name="Border" >
- <Grid>
- <ContentPresenter
- Margin="20,3,6,10"
- ContentSource="Header"
- RecognizesAccessKey="True" />
- </Grid>
- </Border>
- <ControlTemplate.Triggers>
- <Trigger Property="IsHighlighted" Value="true">
- <Setter TargetName="Border" Property="Background" Value="Blue"/>
- <Setter TargetName="Border" Property="BorderBrush" Value="Transparent"/>
- </Trigger>
- <Trigger Property="IsEnabled" Value="False">
- <Setter Property="Foreground" Value="#888888"/>
- </Trigger>
- </ControlTemplate.Triggers>
- </ControlTemplate>
-
- <!-- SubmenuItem 自定义菜单项的外观样式-->
- <ControlTemplate
- x:Key="{x:Static MenuItem.SubmenuItemTemplateKey}"
- TargetType="{x:Type MenuItem}">
- <Border Name="Border" >
- <Grid >
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="Auto" SharedSizeGroup="Icon"/>
- <ColumnDefinition Width="*" />
- <ColumnDefinition Width="Auto" SharedSizeGroup="Shortcut"/>
- <ColumnDefinition Width="13"/>
- </Grid.ColumnDefinitions>
- <ContentPresenter
- Name="Icon"
- Margin="6,2,6,2"
- HorizontalAlignment="Stretch"
- VerticalAlignment="Stretch"
- ContentSource="Icon"/>
- <Border
- Name="Check"
- Width="13" Height="13"
- Visibility="Hidden"
- Margin="6,0,6,0"
- Background="#C0C0C0"
- BorderThickness="1"
- BorderBrush="#404040">
- <Path
- Name="CheckMark"
- Width="7" Height="7"
- Visibility="Visible"
- SnapsToDevicePixels="False"
- Stroke="#404040"
- StrokeThickness="1"
- Data="M 0 0 L 7 7 M 0 7 L 7 0" />
- </Border>
- <ContentPresenter
- Name="HeaderHost"
- Grid.Column="1"
- ContentSource="Header"
- RecognizesAccessKey="True"/>
- <TextBlock x:Name="InputGestureText"
- Grid.Column="2"
- Background="Green"
- Text="{TemplateBinding InputGestureText}"
- Margin="5,2,0,2"
- DockPanel.Dock="Right" />
- </Grid>
- </Border>
- <ControlTemplate.Triggers>
- <Trigger Property="Icon" Value="{x:Null}">
- <Setter TargetName="Icon" Property="Visibility" Value="Hidden"/>
- </Trigger>
- <Trigger Property="IsChecked" Value="true">
- <Setter TargetName="CheckMark" Property="Visibility" Value="Visible"/>
- </Trigger>
- <Trigger Property="IsCheckable" Value="true">
- <Setter TargetName="Check" Property="Visibility" Value="Hidden"/>
- <Setter TargetName="Icon" Property="Visibility" Value="Hidden"/>
- </Trigger>
- <!--选中菜单项时的高亮颜色的trigger设置-->
- <Trigger Property="IsHighlighted" Value="true">
- <Setter TargetName="Border" Property="Background" Value="Green"/>
- </Trigger>
- <Trigger Property="IsEnabled" Value="false">
- <Setter Property="Foreground" Value="Yellow"/>
- </Trigger>
- </ControlTemplate.Triggers>
- </ControlTemplate>
-
- <!-- SubmenuHeader -->
-
- <ControlTemplate
- x:Key="{x:Static MenuItem.SubmenuHeaderTemplateKey}"
- TargetType="{x:Type MenuItem}">
- <Border Name="Border" >
- <Grid>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="Auto" SharedSizeGroup="Icon"/>
- <ColumnDefinition Width="*" />
- <ColumnDefinition Width="Auto" SharedSizeGroup="Shortcut"/>
- <ColumnDefinition Width="13"/>
- </Grid.ColumnDefinitions>
- <ContentPresenter
- Name="Icon"
- Margin="6,0,6,0"
- HorizontalAlignment="Stretch"
- VerticalAlignment="Stretch"
- ContentSource="Icon"/>
- <ContentPresenter
- Name="HeaderHost"
- Grid.Column="1"
- ContentSource="Header"
- RecognizesAccessKey="True"/>
- <TextBlock x:Name="InputGestureText"
- Grid.Column="2"
- Text="{TemplateBinding InputGestureText}"
- Margin="5,2,2,2"
- DockPanel.Dock="Right"/>
- <Path
- Grid.Column="3"
- HorizontalAlignment="Center"
- VerticalAlignment="Center"
- Data="M 0 0 L 0 7 L 4 3.5 Z"
- Fill="#404040" />
- <Popup
- Name="Popup"
- Placement="Left"
- HorizontalOffset="-4"
- IsOpen="{TemplateBinding IsSubmenuOpen}"
- AllowsTransparency="True"
- Focusable="False"
- PopupAnimation="Slide">
- <Border
- Name="SubmenuBorder"
- SnapsToDevicePixels="True"
- Background="#FFFFFF"
- BorderBrush="#888888"
- BorderThickness="1" >
- <StackPanel
- IsItemsHost="True"
- KeyboardNavigation.DirectionalNavigation="Cycle" />
- </Border>
- </Popup>
- </Grid>
- </Border>
- <ControlTemplate.Triggers>
- <Trigger Property="Icon" Value="{x:Null}">
- <Setter TargetName="Icon" Property="Visibility" Value="Collapsed"/>
- </Trigger>
- <Trigger Property="IsHighlighted" Value="true">
- <Setter TargetName="Border" Property="Background" Value="#EEEEEE"/>
- </Trigger>
- <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="True">
- <Setter TargetName="SubmenuBorder" Property="CornerRadius" Value="4"/>
- <Setter TargetName="SubmenuBorder" Property="Padding" Value="0,3,0,3"/>
- </Trigger>
- <Trigger Property="IsEnabled" Value="false">
- <Setter Property="Foreground" Value="#888888"/>
- </Trigger>
- </ControlTemplate.Triggers>
- </ControlTemplate>
-
- <!-- MenuItem Style -->
-
- <Style x:Key="{x:Type MenuItem}" TargetType="{x:Type MenuItem}">
- <Setter Property="OverridesDefaultStyle" Value="True"/>
- <Style.Triggers>
- <Trigger Property="Role" Value="TopLevelHeader">
- <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.TopLevelHeaderTemplateKey}}"/>
- <Setter Property="Grid.IsSharedSizeScope" Value="true"/>
- </Trigger>
- <Trigger Property="Role" Value="TopLevelItem">
- <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.TopLevelItemTemplateKey}}"/>
- </Trigger>
- <Trigger Property="Role" Value="SubmenuHeader">
- <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.SubmenuHeaderTemplateKey}}"/>
- </Trigger>
- <Trigger Property="Role" Value="SubmenuItem">
- <Setter Property="Template" Value="{StaticResource {x:Static MenuItem.SubmenuItemTemplateKey}}"/>
- </Trigger>
- </Style.Triggers>
- </Style>

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。