XAML 我希望WPF装饰器只绘制在所需的区域

r8uurelv  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(121)

如果您从画布中获取AdornerLayer并绘制装饰器,它仍将绘制在画布之外。

if(newAdorner != null)
                    {
                        AdornerLayer.GetAdornerLayer(_drawingCanvas).Add(newAdorner);
                        _adorners.Add(shapeItem.Uid, newAdorner);
                    }
<!--<AdornerDecorator x:Name="DrawingAdorner">-->
            <Canvas x:Name="DrawingCanvas" Background="Transparent">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseLeftButtonDown">
                        <prism:InvokeCommandAction Command="{Binding CanvasLButtonDownCmd}" />
                    </i:EventTrigger>
                    <i:EventTrigger EventName="MouseLeftButtonUp">
                        <prism:InvokeCommandAction Command="{Binding CanvasLButtonUpCmd}" />
                    </i:EventTrigger>
                    <i:EventTrigger EventName="MouseMove">
                        <prism:InvokeCommandAction Command="{Binding CanvasMouseMoveCmd}" />
                    </i:EventTrigger>
                    <i:EventTrigger EventName="SizeChanged">
                        <prism:InvokeCommandAction Command="{Binding CanvasSizeChangedCmd}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Canvas>
        <!--</AdornerDecorator>-->

enter image description here
如何使装饰器只出现在画布区域内?
我试试这个。

<AdornerDecorator x:Name="DrawingAdorner">
            <Canvas x:Name="DrawingCanvas" Background="Transparent">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseLeftButtonDown">
                        <prism:InvokeCommandAction Command="{Binding CanvasLButtonDownCmd}" />
                    </i:EventTrigger>
                    <i:EventTrigger EventName="MouseLeftButtonUp">
                        <prism:InvokeCommandAction Command="{Binding CanvasLButtonUpCmd}" />
                    </i:EventTrigger>
                    <i:EventTrigger EventName="MouseMove">
                        <prism:InvokeCommandAction Command="{Binding CanvasMouseMoveCmd}" />
                    </i:EventTrigger>
                    <i:EventTrigger EventName="SizeChanged">
                        <prism:InvokeCommandAction Command="{Binding CanvasSizeChangedCmd}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Canvas>
        </AdornerDecorator>

enter image description here

monwx1rj

monwx1rj1#

代码在修改后正常工作,如下所示。

<AdornerDecorator>
            <Canvas x:Name="DrawingCanvas" Background="Transparent">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseLeftButtonDown">
                        <prism:InvokeCommandAction Command="{Binding CanvasLButtonDownCmd}" />
                    </i:EventTrigger>
                    <i:EventTrigger EventName="MouseLeftButtonUp">
                        <prism:InvokeCommandAction Command="{Binding CanvasLButtonUpCmd}" />
                    </i:EventTrigger>
                    <i:EventTrigger EventName="MouseMove">
                        <prism:InvokeCommandAction Command="{Binding CanvasMouseMoveCmd}" />
                    </i:EventTrigger>
                    <i:EventTrigger EventName="SizeChanged">
                        <prism:InvokeCommandAction Command="{Binding CanvasSizeChangedCmd}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>

                <ItemsControl x:Name="BoxItems" ItemsSource="{Binding Boxes}">
                </ItemsControl>
                <ItemsControl x:Name="EllipseItems" ItemsSource="{Binding Ellipses}">
                </ItemsControl>
            </Canvas>
        </AdornerDecorator>

我还添加了ClipToBounds=“True”。
enter image description here

相关问题