我在 Canvas 中添加了一个 Thumb 控件,它的行为正是我想要的。然后我想创建多个这样的控件,我使用自定义类和 ControlTemplate 来实现。我遇到的问题是 ControlTemplate 中的代码和外面的代码完全一样,但是 Thumb 不能拖动。
<Canvas x:Name="cnvs" Width="300" Height="300" Background="LightBlue">
<Canvas.Resources>
<Style TargetType="{x:Type current:AnchorPoint}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type current:AnchorPoint}">
<Thumb Width="5" Height="5" DragDelta="Thumb_DragDelta" Cursor="Cross" Canvas.Left="0" Canvas.Top="0">
<Thumb.Template>
<ControlTemplate TargetType="Thumb">
<Grid>
<Rectangle Fill="Transparent" Margin="-3"/>
<Rectangle Stroke="Red" Fill="White" StrokeThickness="1"/>
</Grid>
</ControlTemplate>
</Thumb.Template>
</Thumb>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Canvas.Resources>
<Thumb Width="5" Height="5" DragDelta="Thumb_DragDelta" Cursor="Cross" Canvas.Left="0" Canvas.Top="0">
<Thumb.Template>
<ControlTemplate TargetType="Thumb">
<Grid>
<Rectangle Fill="Transparent" Margin="-3"/>
<Rectangle Stroke="Red" Fill="White" StrokeThickness="1"/>
</Grid>
</ControlTemplate>
</Thumb.Template>
</Thumb>
<Path Stroke="Black" StrokeThickness="1" Name="pth"/>
</Canvas>