郁闷,图片不能上传,我只能先写代码了
实现了一个scorllview 鼠标拖拽 浏览里面内容的功能,留着也许以后用的到下面直接上demo代码
前台
<Window x:Class="scrollviewr_drag.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:scrollviewr_drag" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="30px"></RowDefinition> <RowDefinition Height="*"></RowDefinition> </Grid.RowDefinitions> <TextBlock x:Name="postion" HorizontalAlignment="Left" FontSize="24"></TextBlock> <ScrollViewer Margin="20" Grid.Row="1" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" x:Name="scroller" > <ScrollViewer.Style> <Style TargetType="ScrollViewer"> <Style.Triggers> <Trigger Property="ScrollViewer.IsMouseCaptured" Value="True"> <Setter Property="Cursor" Value="Hand"></Setter> </Trigger> </Style.Triggers> </Style> </ScrollViewer.Style> <Canvas Background="Gainsboro" Width="2000" Height="2000" x:Name="content" MouseMove="UIElement_OnMouseMove" MouseLeftButtonDown="Content_OnMouseLeftButtonDown" MouseLeftButtonUp="Content_OnMouseLeftButtonUp"></Canvas> </ScrollViewer> </Grid> </Window>
后端
using System; using System.Collections.Generic; using System.IO; 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.Resources; namespace scrollviewr_drag { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { /// <summary> /// 用于记录鼠标按下的点 /// </summary> private Point _clickPoint = new Point(0, 0); public MainWindow() { InitializeComponent(); this.Loaded += MainWindow_Loaded; } private void MainWindow_Loaded(object sender, RoutedEventArgs e) { Random rand = new Random(); rand.Next(0, 800); for (int i = 0; i < 1000; i++) { TextBlock t = new TextBlock(); t.FontSize = 20; t.Text = i.ToString(); content.Children.Add(t); Canvas.SetLeft(t, rand.Next(0, 2000)); Canvas.SetTop(t, rand.Next(0, 2000)); } //_clickPoint = new Point(scroller.ActualWidth / 2, scroller.ActualWidth); } private void UIElement_OnMouseMove(object sender, MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { Canvas cabSender = (Canvas)sender; //_clickPoint=new Point(scroll.ActualWidth/2,scroll.ActualHeight/2); double x; double y; Point p = e.MouseDevice.GetPosition(cabSender); x = _clickPoint.X - p.X; y = _clickPoint.Y - p.Y; postion.Text = $"({x},{y})"; scroller.ScrollToHorizontalOffset(scroller.HorizontalOffset+x); scroller.ScrollToVerticalOffset(scroller.VerticalOffset+y); } } private void Content_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { _clickPoint = e.GetPosition((Canvas)sender); this.Cursor = Cursors.Hand; } private void Content_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { this.Cursor = Cursors.Arrow; } } }
所有代码 都在这里 拷走即用