<ItemsControl x:Class="HDSProject.Control.CustomFont" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:HDSProject.Control" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800" x:Name="itemCtrl" ItemsSource="{Binding Path=Title2, RelativeSource={RelativeSource Mode=Self}}" FontSize="20" Foreground="Teal" > <ItemsControl.ItemContainerStyle> <Style TargetType="ContentPresenter"> <Setter Property="VerticalAlignment" Value="Center" /> </Style> </ItemsControl.ItemContainerStyle> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <StackPanel> <Border Margin="{Binding Path=CharacterSpacing, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomFont}}"> <TextBlock Text="{Binding}" /> </Border> <Rectangle Height="1" Fill="{Binding Foreground, ElementName=itemCtrl}" Margin="0,0,0,5"> <Rectangle.Style> <Style TargetType="Rectangle"> <Setter Property="Visibility" Value="Collapsed" /> <Style.Triggers> <DataTrigger Binding="{Binding ShowUnderline, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomFont}}" Value="True"> <Setter Property="Visibility" Value="Visible" /> </DataTrigger> </Style.Triggers> </Style> </Rectangle.Style> </Rectangle> </StackPanel> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>给这个自定义控件添加Thumb
时间: 2023-07-19 15:29:11 浏览: 162
MSDN_Magazine_2010.06(MSDN杂志)
好的,可以使用Thumb来实现拖动自定义控件的功能。具体步骤如下:
1. 在自定义控件的XAML文件中添加Thumb控件。
```xaml
<ItemsControl x:Class="HDSProject.Control.CustomFont" ...>
<ItemsControl.Template>
<ControlTemplate TargetType="ItemsControl">
<Grid>
<Thumb x:Name="PART_Thumb" DragDelta="Thumb_DragDelta" />
...
</Grid>
</ControlTemplate>
</ItemsControl.Template>
...
</ItemsControl>
```
2. 在自定义控件的代码文件中添加Thumb_DragDelta事件处理程序。
```csharp
public class CustomFont : ItemsControl
{
private Thumb _thumb;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_thumb = GetTemplateChild("PART_Thumb") as Thumb;
}
private void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
{
Canvas.SetLeft(this, Canvas.GetLeft(this) + e.HorizontalChange);
Canvas.SetTop(this, Canvas.GetTop(this) + e.VerticalChange);
}
}
```
3. 在Thumb_DragDelta事件处理程序中实现自定义控件的拖动功能。您可以使用Canvas.SetLeft和Canvas.SetTop方法来设置自定义控件的位置。
希望这个解决方案能够解决您的问题。
阅读全文