我是UWP canvas元素的新手。从我找到实现它的方式的那一刻起。我找到了这个链接this link,它实现了我想要的控件。但是我遵循在其中定义的类。我无法继承显示它是密封类型的Thumb控件。有没有人知道如何找到实现这个目标的方法?
public class MoveThumb : Thumb
{
public MoveThumb(Control dataContext, DragDeltaEventHandler dragDelta)
{
DataContext = dataContext;
DragDelta = dragDelta;
}
public Control DataContext { get; private set; }
public DragDeltaEventHandler DragDelta { get; }
private void MoveThumb_DragDelta(object sender, DragDeltaEventArgs e)
{
Control designerItem = this.DataContext as Control;
if (designerItem != null)
{
double left = Canvas.GetLeft(designerItem);
double top = Canvas.GetTop(designerItem);
Canvas.SetLeft(designerItem, left + e.HorizontalChange);
Canvas.SetTop(designerItem, top + e.VerticalChange);
}
}
}
答案 0 :(得分:6)
我有一些样品。认为你会得到这个想法。这里是UserControl
XAML:
Width="256"
Height="128">
<Grid x:Name="ContainerGrid" Background="Transparent"
ManipulationMode="TranslateX,TranslateY"
ManipulationStarted="Manipulator_OnManipulationStarted"
ManipulationDelta="Manipulator_OnManipulationDelta">
<Viewbox IsHitTestVisible="False">
<TextBlock Text="Text" Foreground="Lime"/>
</Viewbox>
<Rectangle x:Name="ResizeRectangle" IsHitTestVisible="False"
Width="16" Height="16" Fill="Orange"
VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
<Rectangle Stretch="Fill" Stroke="DeepPink" StrokeThickness="2"
IsHitTestVisible="False"/>
</Grid>
背后的代码:
private bool _isResizing;
public Manipulator()
{
InitializeComponent();
}
private void Manipulator_OnManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
{
if (e.Position.X > Width - ResizeRectangle.Width && e.Position.Y > Height - ResizeRectangle.Height) _isResizing = true;
else _isResizing = false;
}
private void Manipulator_OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
if (_isResizing)
{
Width += e.Delta.Translation.X;
Height += e.Delta.Translation.Y;
}
else
{
Canvas.SetLeft(this, Canvas.GetLeft(this) + e.Delta.Translation.X);
Canvas.SetTop(this, Canvas.GetTop(this) + e.Delta.Translation.Y);
}
}
创建此UserControl
后,只需将其放入Canvas
:
<Canvas>
<local:Manipulator/>
</Canvas>
我称之为Manipuilator。
以下是它的样子: