WPF使用多个ContextMenu菜单并绑定指定控件右键事件触发菜单,以及后台获取ContextMenu的子项

前端代码

<Window x:Class="EVES_ManualTest.CurveForm"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:wf="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
        xmlns:local="clr-namespace:EVES_ManualTest" Background="Transparent"
        mc:Ignorable="d" WindowStyle="None" ResizeMode="CanMinimize"
        Title="CurveForm" Height="450" Width="800">
    <Window.Resources>
        <!--ContextMenu圆润-->
        <Style x:Key="RoundContextMenuStyle" TargetType="{x:Type ContextMenu}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ContextMenu}">
                        <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            CornerRadius="4">
                            <StackPanel IsItemsHost="True" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <!--#region 右键菜单-->
        <!--曲线视图右键菜单-->
        <ContextMenu x:Key="viewMenu" Style="{StaticResource RoundContextMenuStyle}" Opened="viewMenu_Opened">
            <MenuItem x:Name="vRoll" Header="坐标轴滚动" />
            <MenuItem x:Name="vScale" Header="坐标轴缩放" />
            <Separator/>
            <MenuItem x:Name="vZoomToFit" Header="缩放合适大小" />
            <MenuItem x:Name="vAutoTrack" Header="自动追踪当前数据" />
            <Separator/>
            <MenuItem x:Name="vCurveMax" Header="曲线放大" />
            <MenuItem x:Name="vCurveMin" Header="曲线缩小" />
            <MenuItem x:Name="vCurvePartMax" Header="曲线局部放大" />
            <MenuItem x:Name="vReCurve" Header="恢复" />
            <Separator/>
            <MenuItem x:Name="vShowCursor" Header="显示光标" />
            <Separator/>
            <MenuItem x:Name="vCurvePrint" Header="打印曲线" />
            <Separator/>
            <MenuItem x:Name="vCurveAttribute" Header="曲线属性" />
        </ContextMenu>
        
        <!--X轴右键菜单-->
        <ContextMenu x:Key="xMenu" Style="{StaticResource RoundContextMenuStyle}">
            <MenuItem x:Name="xRoll" Header="坐标轴滚动" />
            <MenuItem x:Name="xScale" Header="坐标轴缩放" />
            <Separator/>
            <MenuItem x:Name="xStartPoint" Header="定位起始位置" />
            <MenuItem x:Name="xZoomToFit" Header="缩放合适大小" />
            <MenuItem x:Name="xAutoTrack" Header="自动追踪当前数据" />
            <Separator/>
            <MenuItem x:Name="xEdit" Header="X轴编辑" />
        </ContextMenu>

        <!--Y轴右键菜单-->
        <ContextMenu x:Key="yMenu" Style="{StaticResource RoundContextMenuStyle}">
            <MenuItem x:Name="yRoll" Header="坐标轴滚动" />
            <MenuItem x:Name="yScale" Header="坐标轴缩放" />
            <Separator/>
            <MenuItem x:Name="yAutoTrack" Header="自动追踪当前数据" />
            <Separator/>
            <MenuItem x:Name="yEdit" Header="Y轴编辑" />
        </ContextMenu>

        <!--数据光标右键菜单-->
        <ContextMenu x:Key="cursorMenu" Style="{StaticResource RoundContextMenuStyle}">
            <MenuItem x:Name="cAllChannel" Header="全部通道" />
            <Separator/>
            <MenuItem x:Name="cTimerData" Header="时间和数据" />
            <MenuItem x:Name="cTimer" Header="时间" />
            <MenuItem x:Name="cData" Header="数据" />
            <MenuItem x:Name="cTimerDifference" Header="时间差" />
            <MenuItem x:Name="cDataDifference" Header="数据差" />
            <Separator/>
            <MenuItem x:Name="cIntervalStatistics" Header="区间统计" />
            <MenuItem x:Name="cTableIntervalStatistics" Header="区间报表" />
        </ContextMenu>
        <!--#endregion-->
    </Window.Resources>
    <Grid>
        <wf:WindowsFormsHost x:Name="CurvePanel"
                             Width="Auto" Height="Auto" Margin="0,-36,0,0" 
                             Background="Transparent">
        </wf:WindowsFormsHost>
    </Grid>
</Window>

 

后台通过指定控件事件调用(我这后台创建了一个外部控件,并为控件创建了不同区域的鼠标右键事件)

 

        /// <summary>
        /// 对象
        /// </summary>
        public AxiPlotLibrary.AxiPlotX CurveView;


        private void Window_Loaded(object sender, RoutedEventArgs e)//窗口加载事件
        {
            CurveView.OnMouseDownXAxis += CurveView_OnMouseDownXAxis;
            CurveView.OnMouseDownYAxis += CurveView_OnMouseDownYAxis;
            CurveView.OnMouseDownDataView += CurveView_OnMouseDownDataView;
            CurveView.OnMouseDownDataCursor += CurveView_OnMouseDownDataCursor;
        }



        #region    右键菜单
        private void CurveView_OnMouseDownXAxis(object sender, AxiPlotLibrary.IiPlotXEvents_OnMouseDownXAxisEvent e)//右键X轴菜单
        {
            if (e.button == 2)//如果按钮为右键
            {
                ContextMenu menu = (ContextMenu)FindResource("xMenu");
                menu.PlacementTarget = (UIElement)CurvePanel;
                menu.IsOpen = true;
            }
        }

        private void CurveView_OnMouseDownYAxis(object sender, AxiPlotLibrary.IiPlotXEvents_OnMouseDownYAxisEvent e)//右键Y轴菜单
        {
            if (e.button == 2)//如果按钮为右键
            {
                if (e.button == 2)//如果按钮为右键
                {
                    ContextMenu menu = (ContextMenu)FindResource("yMenu");
                    menu.PlacementTarget = (UIElement)CurvePanel;
                    menu.IsOpen = true;
                }
            }
        }

        private void CurveView_OnMouseDownDataView(object sender, AxiPlotLibrary.IiPlotXEvents_OnMouseDownDataViewEvent e)//右键数据视图菜单
        {
            if (e.button == 2)//如果按钮为右键,显示数据视图菜单
            {
                ContextMenu menu = (ContextMenu)FindResource("viewMenu");
                menu.PlacementTarget = (UIElement)CurvePanel;
                menu.IsOpen = true;
            }
        }

        private void CurveView_OnMouseDownDataCursor(object sender, AxiPlotLibrary.IiPlotXEvents_OnMouseDownDataCursorEvent e)//右键数据光标菜单
        {
            if (e.button == 2)//如果按钮为右键
            {
                ContextMenu menu = (ContextMenu)FindResource("cursorMenu");//搜索菜单资源对象
                menu.PlacementTarget = (UIElement)CurvePanel;//绑定显示区域
                menu.IsOpen = true;//进行显示
            }
        }
        #endregion
        

        //以下包含使用ContextMenu子项
        #region    右键菜单打开事件
        private void viewMenu_Opened(object sender, RoutedEventArgs e)//打开视图菜单时发生
        {
            ContextMenu viewMenu = FindResource("viewMenu") as ContextMenu;
            MenuItem vRoll = LogicalTreeHelper.FindLogicalNode(viewMenu, "vRoll") as MenuItem;//滚动状态
            MenuItem vScale = LogicalTreeHelper.FindLogicalNode(viewMenu, "vScale") as MenuItem;//缩放状态
            MenuItem vAutoTrack = LogicalTreeHelper.FindLogicalNode(viewMenu, "vAutoTrack") as MenuItem;//自动跟踪选中状态
            MenuItem vCurvePartMax = LogicalTreeHelper.FindLogicalNode(viewMenu, "vCurvePartMax") as MenuItem;//局部放大开启状态
            MenuItem vShowCursor = LogicalTreeHelper.FindLogicalNode(viewMenu, "vShowCursor") as MenuItem;//数据光标选中状态

            vRoll.IsChecked = RollJudge;//设置滚动选中状态
            vScale.IsChecked = ScaleJudge;//设置缩放选中状态
            AutoTracking = GetAllAutoTrackingEnable();//获取当前所有自动跟踪开启状态
            vAutoTrack.IsChecked = AutoTracking;//设置当前所有自动跟踪选中状态
            vCurvePartMax.IsChecked = CurveView.get_ToolBar(0).ZoomBoxActive;//获取局部放大开启状态,开启为true,关闭为false
            vShowCursor.IsChecked = CurveView.get_DataCursor(0).Visible;//设置数据光标选中状态
        }

        private void xMenu_Opened(object sender, RoutedEventArgs e)//打开X轴菜单时发生
        {
            ContextMenu xMenu = FindResource("xMenu") as ContextMenu;
            MenuItem xRoll = LogicalTreeHelper.FindLogicalNode(xMenu, "xRoll") as MenuItem;//滚动状态
            MenuItem xScale = LogicalTreeHelper.FindLogicalNode(xMenu, "xScale") as MenuItem;//缩放状态
            MenuItem xAutoTrack = LogicalTreeHelper.FindLogicalNode(xMenu, "xAutoTrack") as MenuItem;//自动跟踪选中状态

            xRoll.IsChecked = RollJudge;//设置滚动选中状态
            xScale.IsChecked = ScaleJudge;//设置缩放选中状态
            AutoTracking = CurveView.get_XAxis(0).TrackingEnabled;//获取当前选中X轴自动跟踪开启状态
            xAutoTrack.IsChecked = AutoTracking;//设置当前选中X轴自动跟踪选中状态
        }

        private void yMenu_Opened(object sender, RoutedEventArgs e)//打开Y轴菜单时发生
        {
            ContextMenu yMenu = FindResource("yMenu") as ContextMenu;
            MenuItem yRoll = LogicalTreeHelper.FindLogicalNode(yMenu, "yRoll") as MenuItem;//滚动状态
            MenuItem yScale = LogicalTreeHelper.FindLogicalNode(yMenu, "yScale") as MenuItem;//缩放状态
            MenuItem yAutoTrack = LogicalTreeHelper.FindLogicalNode(yMenu, "yAutoTrack") as MenuItem;//自动跟踪选中状态

            yRoll.IsChecked = RollJudge;//设置滚动选中状态
            yScale.IsChecked = ScaleJudge;//设置缩放选中状态
            AutoTracking = CurveView.get_XAxis(0).TrackingEnabled;//获取当前选中X轴自动跟踪开启状态
            yAutoTrack.IsChecked = AutoTracking;//设置当前选中X轴自动跟踪选中状态
        }

        private void cursorMenu_Opened(object sender, RoutedEventArgs e)//打开数据光标菜单时发生
        {

        }
        #endregion

 

posted @ 2023-11-24 09:41  青丝·旅人  阅读(891)  评论(0编辑  收藏  举报