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" >
            <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" >
            <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.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">
                  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.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"/>
                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>
WPF中的右键菜单主要是通过ContextMenu来实现,ContextMenu的使用有两种方式一、.在Resources中声明,在控件中使用。1.在Resources中添加ContextMenu &lt;Window.Resources&gt; &lt;ContextMenu x:Key="ContextMenu"&gt; &lt;MenuItem Name="MiFullScreen" Header="全屏"/&gt;
WPF 控件右侧状态栏实现ContextMenu1、在Resources中声明,在控件中使用2、在控件中直接声明 ContextMenu WPF 中ContextMenu表示鼠标 右键 菜单 栏,适用于 WPF 任何控件。ContextMenu属性默认实现,鼠标 右键 点击后显示 菜单 栏的功能,故使用ContextMenu属性时只需要安心实现 菜单 栏的 样式 和功能即可。 常用的通过ContextMenu实现 右键 菜单 栏的方式有两种。 1、在Resources中声明,在控件中使用 在Window.Resources中声明具体的Con
利用ContextMenu类可以实现 右键 菜单 功能,在任何拥有该属性的控件都可以直接通过该类来实现 右键 功能。以Button为例 1 <Button Name="myButton" Click="myButton_Click"> 2 <Button.ContextMenu> 3 <Co...
private void PushOutMessageBox(object sender, RoutedEventArgs e) MessageBox.Show("弹出一个窗口~然后修改 右键 菜单 显示"); //获取或设置 右键 菜单 信息 (e.Source as MenuItem).Header = "hello world"; //设置背景颜色 SolidColorBrush m_scb = new SolidColorBrush(); m_scb.Color = Color.FromRgb(132,132,132); (e.Source as MenuItem).Background = m_scb; 这其实是一个很简单的小例子,本文讨论使用C#代码动态创建,XAML如何创建不在本文讨论范围。使用的类是ContextMenu。 其实每个控件都有ContextMenu方法,本文就以在一个矩形框右击弹出 菜单 栏。 第一步先创建一个矩形, 第二步创建 菜单 栏显示个内容, 第三步把创建的 菜单 栏添加到矩形中, 第四步创建鼠标右击事件。 var X = (GridHeight + GridBorder) * i + GridBorder; var Y =
要在 WPF DataGrid 中添加 右键 菜单 ,可以使用 ContextMenu 属性。首先,在 DataGrid 中添加 ContextMenu 控件,然后添加 MenuItem 控件作为子控件。以下是一个简单的示例: ```xml <DataGrid Name="myDataGrid"> <DataGrid.ContextMenu> <ContextMenu> <MenuItem Header="Cut"/> <MenuItem Header="Copy"/> <MenuItem Header="Paste"/> </ContextMenu> </DataGrid.ContextMenu> </DataGrid> 在上面的示例中,我们为 DataGrid 添加了一个 ContextMenu,其中包含三个 MenuItem 控件。当用户 右键 单击 DataGrid 时,将显示这个 菜单 。 你也可以通过在代码中为 ContextMenu 添加事件处理程序来实现自定义功能。例如,以下代码将在用户单击 "Cut" 菜单 项时删除选定的行: ```csharp private void MenuItemCut_Click(object sender, RoutedEventArgs e) while (myDataGrid.SelectedItems.Count > 0) myDataGrid.Items.Remove(myDataGrid.SelectedItems[0]); 要在代码中设置 ContextMenu,可以使用以下语法: ```csharp myDataGrid.ContextMenu = new ContextMenu(); 然后,可以通过添加 MenuItem 控件来向 ContextMenu 添加 菜单 项。
最近在弄STM32F429+FreeRTOS+LWIP,还有5天项目要交,十万火急,恳求楼主或收到代码的朋友给一份,或者指点,谢谢! 65650657@qq.com 或 lifesky2006@163.com