我在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>