WPF上下文用于向前推进和向后发送的菜单控件

本文关键字:菜单 控件 用于 上下文 WPF | 更新日期: 2023-09-27 18:23:48

遇到问题,我希望这实际上很简单。

我在WPF中有一个自定义的UserControl,它允许我显示图像。当程序运行时,用户可以随意多次将此UserControl添加到画布中。实际上是一个简单的图像查看器,他们可以在其中添加和移动图像。

我希望能够右键单击这些图像打开上下文菜单,然后选择向后发送或向前发送,然后图像将根据单击的菜单选择更改z顺序。

我已经用contextMenu设置了用户控件,所以我只需要知道更改这个userControl的z顺序的代码。。。

非常感谢您的帮助:)

namespace StoryboardTool
{
    /// <summary>
    /// Interaction logic for CustomImage.xaml
    /// </summary>
    public partial class CustomImage : UserControl
    {
        private Point mouseClick;
        private double canvasLeft;
        private double canvasTop;
        public CustomImage()
        {
            InitializeComponent();
            cusImageControl.SetValue(Canvas.LeftProperty, 0.0);
            cusImageControl.SetValue(Canvas.TopProperty, 0.0);
        }
        public void chooseImage()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Choose Image to Add";
            if (ofd.ShowDialog() == true)
            {
                BitmapImage bImage = new BitmapImage();
                bImage.BeginInit();
                bImage.UriSource = new Uri(ofd.FileName);
                bImage.EndInit();
                image.Width = bImage.Width;
                image.Height = bImage.Height;
                image.Source = bImage;
                image.Stretch = Stretch.Fill;
            }
        }
        private void cusImageControl_LostMouseCapture(object sender, MouseEventArgs e)
        {
            ((CustomImage)sender).ReleaseMouseCapture();
        }
        private void cusImageControl_MouseUp(object sender, MouseButtonEventArgs e)
        {
            ((CustomImage)sender).ReleaseMouseCapture();
            cusImageControl.Cursor = Cursors.Arrow;
        }
        private void cusImageControl_MouseMove(object sender, MouseEventArgs e)
        {
            if ((((CustomImage)sender).IsMouseCaptured) && (cusImageControl.Cursor == Cursors.SizeAll))
            {
                Point mouseCurrent = e.GetPosition(null);
                double Left = mouseCurrent.X - mouseClick.X;
                double Top = mouseCurrent.Y - mouseClick.Y;
                mouseClick = e.GetPosition(null);
                ((CustomImage)sender).SetValue(Canvas.LeftProperty, canvasLeft + Left);
                ((CustomImage)sender).SetValue(Canvas.TopProperty, canvasTop + Top);
                canvasLeft = Canvas.GetLeft(((CustomImage)sender));
                canvasTop = Canvas.GetTop(((CustomImage)sender));
            }
            else if ((((CustomImage)sender).IsMouseCaptured) && (cusImageControl.Cursor == Cursors.SizeNWSE))
            {
                /*Point mouseCurrent = e.GetPosition(null);
                cusImageControl.Height = cusImageControl.canvasTop + mouseClick.Y;
                cusImageControl.Width = cusImageControl.canvasLeft + mouseClick.X;
                mouseClick = e.GetPosition(null);*/
            }
        }
        private void cusImageControl_MouseDown(object sender, MouseButtonEventArgs e)
        {
            mouseClick = e.GetPosition(null);
            canvasLeft = Canvas.GetLeft(((CustomImage)sender));
            canvasTop = Canvas.GetTop(((CustomImage)sender));
            ((CustomImage)sender).CaptureMouse();
        }
        private void ContextMenuBringForward_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Bring Forward");
        }
        private void ContextMenuSendBackward_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Send Backward");
        }
        private void ContextMenuMove_Click(object sender, RoutedEventArgs e)
        {
            cusImageControl.Cursor = Cursors.SizeAll;
        }   
        private void ContextMenuResize_Click(object sender, RoutedEventArgs e)
        {
            cusImageControl.Cursor = Cursors.SizeNWSE;
        }
    }
}

WPF上下文用于向前推进和向后发送的菜单控件

请参阅面板附带的属性Canvas.SetZIndex。将所有元素的z-Index更改为0,右键单击控件的z-Index则更改为1。

void mouseUp(object sender, MouseButtonEventArgs e)
{
   foreach (var child in yourCanvas.Children) Canvas.SetZIndex(child, 0); 
   Canvas.SetZIndex((UIElement)sender, 1);
}

以下代码在selected被定义为我的UserControl并在mouseDown事件中设置的地方工作。

private void ContextMenuSendBackward_Click(object sender, RoutedEventArgs e)
        {
            Canvas parent = (Canvas)LogicalTreeHelper.GetParent(this);
            foreach (var child in parent.Children)
            {
                Canvas.SetZIndex((UIElement)child, 0);
            }
            Canvas.SetZIndex(selected, 1);
        }

感谢沃的帮助。