WPF Thumb

Thumb 类,表示可由用户拖动的控件。其主要三个事件分别DragDelta,DragStarted,DragCompleted。

DragDelta —— 当 Thumb 控件具有逻辑焦点和鼠标捕获时,随着鼠标位置更改发生一次或多次。

DragStarted —— 在 Thumb 控件接收逻辑焦点和鼠标捕获时发生。

DragCompleted —— 在 Thumb 控件失去鼠标捕获时发生。

示例:

 

XML:

<Grid>
    <Canvas Panel.ZIndex="2" Height="650" Width="1200">
        <Thumb x:Name="thumbGift" DragDelta="Thumb_DragDelta" BorderBrush="{StaticResource BorderDashedBrushStyle}" BorderThickness="0" DragStarted="Thumb_DragStarted" DragCompleted="Thumb_DragCompleted">
            <Thumb.Template>
                <ControlTemplate>
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                        <Image Stretch="Fill" MaxHeight="650" gif:ImageBehavior.AnimatedSource="pack://application:,,,/Images/gif/xs.gif" />
                    </Border>
                </ControlTemplate>
            </Thumb.Template>
        </Thumb>
    </Canvas>
</Grid>

 

Thumb事件:

private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
    UIElement thumb = e.Source as UIElement;
    Canvas.SetLeft(thumb, Canvas.GetLeft(thumb) + e.HorizontalChange);
    Canvas.SetTop(thumb, Canvas.GetTop(thumb) + e.VerticalChange);
}
private void Thumb_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
    thumbGift.BorderThickness = new Thickness(1);
}

private void Thumb_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
    thumbGift.BorderThickness = new Thickness(0);
}

  

Border虚线:

<DrawingBrush x:Key="BorderDashedBrushStyle" Viewport="0,0,8,8" ViewportUnits="Absolute" TileMode="Tile">
    <DrawingBrush.Drawing>
        <DrawingGroup>
            <GeometryDrawing Brush="#ccc">
                <GeometryDrawing.Geometry>
                    <GeometryGroup>
                        <RectangleGeometry Rect="0,0,50,50" />
                        <RectangleGeometry Rect="50,50,50,50" />
                    </GeometryGroup>
                </GeometryDrawing.Geometry>
            </GeometryDrawing>
        </DrawingGroup>
    </DrawingBrush.Drawing>
</DrawingBrush>

  

 

posted @ 2021-05-28 17:16  microsoft-zhcn  阅读(1344)  评论(0编辑  收藏  举报