使画布背景(使用DrawingBrush)鼠标互动的最简单方法是什么?

时间:2017-05-16 18:49:19

标签: wpf xaml canvas

我使用Canvas Background作为书写板。我想附上 将光标移入,移入或移出画布时的行为中的PreviewMouseMove事件。类似的东西:

canvas.PreviewMouseMove += Canvas_PreviewMouseMove;

Ofcource,Canvas.Background是一个画笔,因此我(目前)使用绘图画笔将几何定义为:

XAML:
 <Canvas x:Name="MainCanvas" Grid.Column="1"  Grid.Row="1" >
                    <Canvas.Background>
                        <DrawingBrush Stretch="None" TileMode="Tile"  ViewportUnits="Absolute">
                            <DrawingBrush.Viewport>
                                <MultiBinding>
                                    <MultiBinding.Converter>
                                        <conv:RectConverter/>
                                    </MultiBinding.Converter>
                                    <Binding Path="DrawingLayer.MainCanvas.ViewPortWidth"/>
                                    <Binding Path="DrawingLayer.MainCanvas.ViewPortHeight"/>
                                </MultiBinding>
                            </DrawingBrush.Viewport>
                            <DrawingBrush.Drawing>
                                <DrawingGroup>
                                    <DrawingGroup.Children>
                                        <GeometryDrawing>
                                            <GeometryDrawing.Brush>
                                                <SolidColorBrush Color="Purple" Opacity="0.2" />
                                            </GeometryDrawing.Brush>
                                            <GeometryDrawing.Pen>
                                                <Pen Thickness="1" Brush="LightGreen"/>
                                            </GeometryDrawing.Pen>
                                            <GeometryDrawing.Geometry>
                                                <RectangleGeometry>
                                                    <RectangleGeometry.Rect>
                                                        <MultiBinding>
                                                            <MultiBinding.Converter>
                                                                <conv:DrawingBoxConverter/>
                                                            </MultiBinding.Converter>
                                                            <Binding Path="DrawingLayer.MainCanvas.DrawingBoxTop"/>
                                                            <Binding Path="DrawingLayer.MainCanvas.ViewPortWidth"/>
                                                            <Binding Path="DrawingLayer.MainCanvas.DrawingBoxHeight"/>
                                                        </MultiBinding>
                                                    </RectangleGeometry.Rect>
                                                </RectangleGeometry>
                                            </GeometryDrawing.Geometry>
                                        </GeometryDrawing>
                                        <GeometryDrawing>
                                            <GeometryDrawing.Geometry>
                                                <GeometryGroup>
                                                    <LineGeometry StartPoint="0,0" EndPoint="{Binding DrawingLayer.MainCanvas.EndPointHeight}"/>
                                                    <LineGeometry StartPoint="0,0" EndPoint="{Binding DrawingLayer.MainCanvas.EndPointWidth}"/>
                                                </GeometryGroup>
                                            </GeometryDrawing.Geometry>
                                            <GeometryDrawing.Pen>
                                                <Pen Thickness="1" Brush="Green"/>
                                            </GeometryDrawing.Pen>
                                        </GeometryDrawing>
                                        <GeometryDrawing>
                                            <GeometryDrawing.Geometry>
                                                <GeometryGroup>
                                                    <LineGeometry StartPoint="{Binding DrawingLayer.MainCanvas.StartPointMidline}" 
                                                                  EndPoint="{Binding DrawingLayer.MainCanvas.EndPointMidline}"/>
                                                </GeometryGroup>
                                            </GeometryDrawing.Geometry>
                                            <GeometryDrawing.Pen>
                                                <Pen Thickness="1" Brush="Red">
                                                    <Pen.DashStyle>
                                                        <DashStyle Dashes="4,12"/>
                                                    </Pen.DashStyle>
                                                    </Pen>
                                            </GeometryDrawing.Pen>
                                        </GeometryDrawing>
                                    </DrawingGroup.Children>
                                </DrawingGroup>
                            </DrawingBrush.Drawing>
                        </DrawingBrush>
                    </Canvas.Background>

这会产生很好的背景:

enter image description here

但是现在我如何允许PreviewMouseMove的鼠标事件?我发现的所有解决方案都说明了背景透明。那么我不应该把垫子放在背景中吗?如果不在后台,这对Viewport有何影响? (我已经阅读了使用Path.Data解决问题的地方,但后来我不知道如何将Path.Data与视口结合起来)。

可以在XAML中使用Border元素(以某种形式)来解决问题吗?

最终,我想要实现的是一个干净的垫(使用画布),它响应鼠标输入,鼠标移动和鼠标离开事件。 (这不应该是一个很大的问题。)

非常感谢任何帮助或建议。

注意:我不需要与几何体交互,只知道光标在画布内的位置。

TIA。

编辑#1。 (请原谅代码墙)。这是RectConverter:

 public class RectConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            return new Rect(0d, 0d, (double)values[0], (double)values[1]);
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

0 个答案:

没有答案