WPF thumb Drag DragDelta,DragStarted,DragCompleted
//xaml <Window x:Class="WpfApp302.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp302" mc:Ignorable="d" WindowState="Maximized" Title="MainWindow" Height="450" Width="800"> <Window.Resources> <Style TargetType="Thumb"> </Style> </Window.Resources> <Grid> <Canvas x:Name="cvs" Panel.ZIndex="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <Thumb x:Name="tb" Canvas.Left="100" Canvas.Top="0" Background="Blue" Width="15" Height="{Binding Path=ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Canvas}}" DragDelta="tb_DragDelta" DragStarted="tb_DragStarted" DragCompleted="tb_DragCompleted"> <Thumb.Template> <ControlTemplate> <Border BorderBrush="Black" BorderThickness="2"> <Rectangle Fill="Blue"/> </Border> </ControlTemplate> </Thumb.Template> </Thumb> </Canvas> </Grid> </Window> //cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApp302 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); LinearGradientBrush lineBrush = new LinearGradientBrush(); lineBrush.StartPoint = new Point(0, 0); lineBrush.EndPoint = new Point(1, 1); GradientStop gs1 = new GradientStop(); gs1.Offset = 0; gs1.Color = Colors.Red; lineBrush.GradientStops.Add(gs1); GradientStop gs2 = new GradientStop(); gs2.Offset = 0.25; gs2.Color = Colors.Brown; lineBrush.GradientStops.Add(gs2); GradientStop gs3 = new GradientStop(); gs3.Offset = 0.5; gs3.Color = Colors.OrangeRed; lineBrush.GradientStops.Add(gs3); GradientStop gs4=new GradientStop(); gs4.Offset = 0.75; gs4.Color = Colors.Orange; lineBrush.GradientStops.Add(gs4); GradientStop gs5 = new GradientStop(); gs5.Offset = 1; gs5.Color = Colors.Yellow; lineBrush.GradientStops.Add(gs5); cvs.Background= lineBrush; } private void tb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e) { double xValue = Canvas.GetLeft(tb) + e.HorizontalChange; Canvas.SetLeft(tb, xValue); //Canvas.SetTop(tb, Canvas.GetTop(tb) + e.VerticalChange); this.Title = $"X:{xValue}"; double yadjust = cvs.Height + e.VerticalChange; double xadjust = cvs.Width + e.HorizontalChange; if((xadjust>=0) && (yadjust>=0)) { } } private void tb_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e) { tb.Background = Brushes.Orange; } private void tb_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e) { tb.Background = Brushes.Blue; } } }
分类:
wpf
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
2019-08-28 convert datatable to List<T>
2019-08-28 ExcelHelper based on NPOI