1、定义一个Dragthumb模板
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DeepLearningEditor">
<!-- DragThumb Default Template -->
<Style TargetType="{x:Type local:DragThumb}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:DragThumb}">
<Rectangle Fill="Transparent"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- DesignerItem Style -->
<Style TargetType="{x:Type local:DesignerInkItem}">
<Setter Property="MinWidth" Value="10"/>
<Setter Property="MinHeight" Value="10"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:DesignerInkItem}">
<Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
<!-- PART_DragThumb -->
<local:DragThumb x:Name="PART_DragThumb"
Panel.ZIndex="10"
Visibility="Collapsed"
Cursor="Hand"/>
<!-- PART_ContentPresenter -->
<ContentPresenter x:Name="PART_ContentPresenter"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Content="{TemplateBinding ContentControl.Content}"
Margin="{TemplateBinding ContentControl.Padding}"/>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Value="True" Binding="{Binding RelativeSource={RelativeSource Self},Path=IsSelected}">
<Setter TargetName="PART_DragThumb" Property="Visibility" Value="Visible"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
2、编写事件
public DragThumb()
{
base.DragDelta += new DragDeltaEventHandler(DragThumb_DragDelta);
}
void DragThumb_DragDelta(object sender, DragDeltaEventArgs e)
{
DesignerItem designerItem = this.DataContext as DesignerItem;
DesignerCanvas designer = VisualTreeHelper.GetParent(designerItem) as DesignerCanvas;
if (designerItem != null && designer != null && designerItem.IsSelected)
{
double minLeft = double.MaxValue;
double minTop = double.MaxValue;
// we only move DesignerItems
var designerItems = designer.SelectionService.CurrentSelection.OfType<DesignerItem>();
foreach (DesignerItem item in designerItems)
{
double left = Canvas.GetLeft(item);
double top = Canvas.GetTop(item);
minLeft = double.IsNaN(left) ? 0 : Math.Min(left, minLeft);
minTop = double.IsNaN(top) ? 0 : Math.Min(top, minTop);
}
double deltaHorizontal = Math.Max(-minLeft, e.HorizontalChange);
double deltaVertical = Math.Max(-minTop, e.VerticalChange);
foreach (DesignerItem item in designerItems)
{
double left = Canvas.GetLeft(item);
double top = Canvas.GetTop(item);
if (double.IsNaN(left)) left = 0;
if (double.IsNaN(top)) top = 0;
Canvas.SetLeft(item, left + deltaHorizontal);
Canvas.SetTop(item, top + deltaVertical);
}
designer.InvalidateMeasure();
e.Handled = true;
}
}