虽然IsHitTestVisible == true,但WPF IsMouseOver始终为false

时间:2017-08-04 22:11:01

标签: c# wpf canvas

我正在使用WPF创建一个图形编辑器,我正在实现对象之间的类似按钮的切换。

我的图形编辑器非常具体,因为我实际上绘制了一些名为“Images”的东西 - 它们是NSLocale实例中的NSDate个实例,然后用户可以绘制所有东西(称为数字)仅在这些图像中。所有图像都会添加到初始画布中。

因此,在一个初始画布上可以有任意数量的图像以及单个图像中的任意数量的图形,但是只有一个当前图像和一个当前图形,并且所有操作都应用于它们。

初始画布基本上看起来像this

矩形是图像,三角形 - 里面的数字,粗体的元素是“当前”

我面临的一个问题是,在执行期间Canvas个实例添加到初始画布的所有元素和画布本身都有Border UIElement。为所有元素设置背景。

问题是其中的图像或元素都没有IsMouseOver == false

将图像添加到画布的代码。

IsHitTestVisible == true
IsMouseOver == trueStartAction = (Point mouseDownPosition) => { _firstPoint = mouseDownPosition; _image = new Border() { Height = 0, Width = 0, Background = Brushes.Transparent }; Canvas.SetLeft(_image, mouseDownPosition.X); Canvas.SetTop(_image, mouseDownPosition.Y); _subject.Canvas.Children.Add(_image); _subject.CurrentImage = _image; }; ContinueAction = (Point currentCursorPosition) => { double width = currentCursorPosition.X - _firstPoint.X; double height = currentCursorPosition.Y - _firstPoint.Y; if (width >= 0) _image.Width = width; else { _image.Width = Math.Abs(width); Canvas.SetLeft(_image, _firstPoint.X + width); } if (height >= 0) _image.Height = height; else { _image.Height = Math.Abs(height); Canvas.SetTop(_image, _firstPoint.Y + height); } }; FinishAction = (mouseUpPosition) => { if (_image.Width == 0 || _image.Height == 0 || _image.BorderThickness.Left + _image.BorderThickness.Right >= _image.Width || _image.BorderThickness.Top + _image.BorderThickness.Bottom >= _image.Height) { _subject.Canvas.Children.Remove(_image); } else { _image.Child = new Canvas() { Width = _image.Width - _image.BorderThickness.Left - _image.BorderThickness.Right, Height = _image.Height - _image.BorderThickness.Top - _image.BorderThickness.Bottom, Background = Brushes.Transparent, ClipToBounds = true, IsHitTestVisible = true }; _subject.CurrentImage = _image; } // we reset variables Reset(); }; - StartActionMouseDown - ContinueAction上调用

MouseMoveFinishAction是Model类的实例,MouseUp_subject实例,在_image类中使用。

应切换当前图像和/或当前图形的代码。

Border

我试图在不那么复杂的环境中模拟情况并且效果很好。

XAML:

ImageDrawingState

代码背后:

FinishAction = (mouseUpPosition) =>
{
    var images = _subject.Canvas.Children;
    Border image = null;
    for (int i = images.Count - 1; i >= 0; i--)
    {
        image = images[i] as Border;
        if (image != null)
        {
            Debug.WriteLine("Image {0}", image.IsMouseOver);
            var position = Mouse.GetPosition(image);
            if (image.IsMouseOver)
                    break;
        }
        image = null;
    }
    if (image != null)
    {
        _subject.CurrentImage = image;
        var figures = (image.Child as Canvas).Children;
        Shape figure = null;
        for (int i = figures.Count - 1; i >= 0; i--)
        {
            figure = figures[i] as Shape;
            Debug.WriteLine("Figure {0}", figure.IsMouseOver);
            if (figure != null)
            {
                if (figure.IsMouseOver)
                    break;
            }
            figure = null;
        }
        if (figure != null)
            _subject.CurrentFigure = figure;
    }
};

那么什么会影响原始程序中的<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="350" Width="525"> <Canvas Name="Canvas" x:FieldModifier="public"> <Label Name="Label" Canvas.Right="100" Canvas.Bottom="20">Nope</Label> </Canvas> </Window> 行为?

提前谢谢。

1 个答案:

答案 0 :(得分:0)

问题是之前由基本画布捕获的鼠标,所以鼠标总是在它上面,而不是在孩子身上。

发布MouseCapture问题后,问题已解决。