WPF应用程序-无法在MouseDown上更改矩形属性

时间:2019-03-14 07:54:11

标签: c# wpf wpf-controls

我有一个简单的WPF应用程序,它是一个窗口应用程序。此窗口上有一个画布。我想要做的是在画布上移动鼠标时,它应该在画布上绘制一个矩形,然后当我按下鼠标左键时,矩形的颜色应该改变。我完全能够在鼠标移动事件上绘制一个矩形,并在Rectangle上接收MouseDown事件,但是当我尝试更改此矩形的颜色时,它不起作用。代码很简单

这是我的xaml文件

<Window x:Class="WpfApp1.MainWindow"
        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:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Canvas Background="#11FFFFFF"  IsHitTestVisible="True" x:Name="overlay" Opacity="1">

        </Canvas>
    </Grid>
</Window>

这是我的xaml.cs文件

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            overlay.MouseMove += OnOverlayMouseMove;
        }

        private void OnOverlayMouseMove(object sender, MouseEventArgs args)
        {
            overlay.Children.Clear();

            Point ps = args.GetPosition(overlay);

            Rectangle rect = new Rectangle
            {
                Fill = Brushes.LightBlue,
                Stroke = Brushes.LightGray,
                StrokeThickness = 2,
                Width = 100,
                Height = 50
            };

            rect.Opacity = 0.5;
            rect.MouseLeftButtonDown += OnRectLeftMouseButtonDown;
            rect.Name = "Blue";

            Canvas.SetLeft(rect, ps.X - 50);
            Canvas.SetTop(rect, ps.Y - 25);
            overlay.Children.Add(rect);
        }

        private void OnRectLeftMouseButtonDown(object sender, MouseEventArgs args)
        {
            Rectangle rect = sender as Rectangle;

            if (rect.Name.Equals("Blue"))
            {
                rect.Fill = Brushes.Black;
                rect.Name = "Black";
            }
            else
            {
                rect.Fill = Brushes.LightBlue;
                rect.Name = "Blue";
            }

            args.Handled = true;
        }
    }
}

2 个答案:

答案 0 :(得分:-1)

颜色没有改变,因为每次调用OnOverlayMouseMove并将“填充”设置为LightBlue时,您都会创建一个新矩形

您可以执行以下操作

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            overlay.MouseMove += OnOverlayMouseMove;
        }

        private void OnOverlayMouseMove(object sender, MouseEventArgs args)
        {
            overlay.Children.Clear();

            Point ps = args.GetPosition(overlay);

            Rectangle rect = new Rectangle
            {
                Fill = brush,
                Stroke = Brushes.LightGray,
                StrokeThickness = 2,
                Width = 100,
                Height = 50
            };

            rect.Opacity = 0.5;
            rect.MouseLeftButtonDown += OnRectLeftMouseButtonDown;
            rect.Name = "Blue";

            Canvas.SetLeft(rect, ps.X - 50);
            Canvas.SetTop(rect, ps.Y - 25);
            overlay.Children.Add(rect);
        }
        private SolidColorBrush brush = Brushes.LightBlue;
        private void OnRectLeftMouseButtonDown(object sender, MouseEventArgs args)
        {
            Rectangle rect = sender as Rectangle;

            if (brush == Brushes.LightBlue)
            {
                brush = Brushes.Black;
            }
            else
            {
                brush = Brushes.LightBlue;
            }

            args.Handled = true;
        }
    }

答案 1 :(得分:-1)

我认为您需要这样的东西:

public partial class MainWindow : Window
{
    private Color normal = Color.FromRgb(255, 0, 0);
    private Color active = Color.FromRgb(0, 0, 0);

    private SolidColorBrush rectangleBrush;


    public MainWindow()
    {
        InitializeComponent();

        rectangleBrush = new SolidColorBrush(normal);
        overlay.MouseMove += OnOverlayMouseMove;
    }

    private void OnOverlayMouseMove(object sender, MouseEventArgs args)
    {
        overlay.Children.Clear();

        Point ps = args.GetPosition(overlay);

        Rectangle rect = new Rectangle
        {
            Fill = rectangleBrush,
            Stroke = Brushes.LightGray,
            StrokeThickness = 2,
            Width = 100,
            Height = 50
        };

        rect.Opacity = 0.5;
        rect.MouseLeftButtonDown += OnRectLeftMouseButtonDown;

        Canvas.SetLeft(rect, ps.X - 50);
        Canvas.SetTop(rect, ps.Y - 25);
        overlay.Children.Add(rect);
    }

    private void OnRectLeftMouseButtonDown(object sender, MouseEventArgs args)
    {
        Rectangle rect = sender as Rectangle;

        if ((rect.Fill as SolidColorBrush).Color == normal) {
            rectangleBrush.Color = active;
        } else {
            rectangleBrush.Color = normal;
        }

        args.Handled = true;
    }
}

如果要使用Color结构中的颜色,只需将普通和活动var替换为“ Colors.LightBlue”和“ Colors.Black”