• 一个可拖拽的多点线段(usercontrol)


    <UserControl x:Class="GmapUserControls.DragLine"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:GmapUserControls"
                 mc:Ignorable="d" 
                 Focusable="True"
                 d:DesignHeight="450" d:DesignWidth="800" Loaded="UserControl_Loaded" >
        <Canvas x:Name="canvas2">
            <Canvas.Resources>
                <ControlTemplate TargetType="Thumb" x:Key="markerTemplate">
                    <Ellipse Fill="{TemplateBinding Background}" 
                                 HorizontalAlignment="Stretch" 
                                 VerticalAlignment="Stretch"/>
                </ControlTemplate>
    
                <Style TargetType="Thumb" x:Key="markerStyle">
                    <Setter Property="Width" Value="10"/>
                    <Setter Property="Height" Value="10" />
                    <Setter Property="Visibility" Value="Hidden"/>
                    <Setter Property="RenderTransform">
                        <Setter.Value>
                            <TranslateTransform X="-5" Y="-5" />
                        </Setter.Value>
                    </Setter>
                    <Setter Property="Cursor" Value="Hand"/>
                    
                    <Setter Property="Template" Value="{StaticResource markerTemplate}" />
                    <EventSetter Event="DragDelta" Handler="Thumb_DragDelta"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=.}" Value="{x:Null}">
                            <Setter Property="Visibility" Value="Hidden" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
                <Style TargetType="{x:Type Line}">
                    <Setter Property="Cursor" Value="Cross"/>
                    <EventSetter Event="MouseLeftButtonDown" Handler="Line_MouseLeftButtonDown" />
                    <EventSetter Event="MouseRightButtonDown" Handler="Line_MouseRightButtonDown"/>
                </Style>
            </Canvas.Resources>
    
    
    
            <Thumb x:Name="marker1" DataContext="{x:Null}"
                       Canvas.Left="{Binding Path=X1,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                       Canvas.Top="{Binding Path=Y1,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                       Background="Red"
                
                       Style="{StaticResource  markerStyle}" />
    
            <Thumb x:Name="marker2"
                       DataContext="{x:Null}"
                       Canvas.Left="{Binding Path=X2,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                       Canvas.Top="{Binding Path=Y2,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                 
                       Background="Red" 
               Style="{StaticResource  markerStyle}" />
        </Canvas>
    </UserControl>
    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 GmapUserControls
    {
        /// <summary>
        /// DragLine.xaml 的交互逻辑
        /// </summary>
        public partial class DragLine : UserControl
        {
            private List<Point> _points;
    
            public List<Point> DragPoints
            {
                get { return _points; }
                set { _points = value; }
            }
            private List<Line> _lines;
            public List<Line> DragLines
            {
                get { return _lines; }
            }
            //Line line;
            public DragLine()
            {
                InitializeComponent();
                _lines = new List<Line>();
            }
            private void UserControl_Loaded(object sender, RoutedEventArgs e)
            {
             
                Line line;
                for (int i = 0; i < _points.Count - 1; i++)
                {
                    Ellipse ellipse1 = new Ellipse() { Width = 5, Height = 5, Fill = Brushes.Red, RenderTransform = new TranslateTransform(-2.5, -2.5) };
                    Ellipse ellipse2 = new Ellipse() { Width = 5, Height = 5, Fill = Brushes.Red, RenderTransform = new TranslateTransform(-2.5, -2.5) };
    
                    line = new Line() { StrokeThickness = 1, Stroke = Brushes.Red };
    
    
                    line.X1 = _points[i].X;
                    line.Y1 = _points[i].Y;
                    line.X2 = _points[i + 1].X;
                    line.Y2 = _points[i + 1].Y;
    
    
                    ellipse1.SetBinding(Canvas.LeftProperty, new Binding("X1") { Source = line, Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
                    ellipse1.SetBinding(Canvas.TopProperty, new Binding("Y1") { Source = line, Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
                    ellipse2.SetBinding(Canvas.LeftProperty, new Binding("X2") { Source = line, Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
                    ellipse2.SetBinding(Canvas.TopProperty, new Binding("Y2") { Source = line, Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
                    var canvas = this.canvas2;
    
                    canvas.Children.Insert(i, line);
                    canvas.Children.Add(ellipse1);
                    canvas.Children.Add(ellipse2);
                    this.marker1.DataContext = line;
                    this.marker2.DataContext = line;
                    _lines.Add(line);
    
                    for (int t = 0; t < _lines.Count - 1; t++)
                    {
    
                        _lines[t].SetBinding(Line.X2Property, new Binding("X1") { Source = _lines[t + 1], Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
                        _lines[t].SetBinding(Line.Y2Property, new Binding("Y1") { Source = _lines[t + 1], Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
                    }
    
                   
    
                }
    
    
    
    
            }
            private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
            {
                var thumb = (System.Windows.Controls.Primitives.Thumb)sender;
                Canvas.SetLeft(thumb, Canvas.GetLeft(thumb) + e.HorizontalChange);
                Canvas.SetTop(thumb, Canvas.GetTop(thumb) + e.VerticalChange);
    
            }
            private void Line_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                marker1.Visibility = Visibility.Visible;
                marker2.Visibility = Visibility.Visible;
                Line line = (Line)sender;
    
                int q = canvas2.Children.IndexOf(line);
    
                this.marker1.DataContext = line;
                this.marker2.DataContext = line;
    
                var p = e.GetPosition((Canvas)line.Parent);
                var p1 = new Point(line.X1, line.Y1);
                var p2 = new Point(line.X2, line.Y2);
    
    
    
                if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
                {
    
    
                    Line newline = new Line();
                    newline.Stroke = line.Stroke;
                    newline.StrokeThickness = line.StrokeThickness;
    
                    System.Windows.Controls.Primitives.Thumb thumb = null;
                    string num = "";
                    if ((p1 - p).Length < 20)
                    {
                        newline.X2 = p1.X;
                        newline.Y2 = p1.Y;
                        num = "1";
    
                    }
                    else if ((p2 - p).Length < 20)
                    {
                        newline.X2 = p2.X;
                        newline.Y2 = p2.Y;
                        num = "2";
                    }
                    else
                    {
                        return;
                    }
    
                    newline.SetBinding(Line.X1Property, new Binding("X" + num) { Source = line, Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
                    newline.SetBinding(Line.Y1Property, new Binding("Y" + num) { Source = line, Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
    
                    var canvas = (Canvas)line.Parent;
                    int index = canvas.Children.IndexOf(marker1);
                    canvas.Children.Insert(index, newline);
    
    
                    Ellipse ellipse1 = new Ellipse() { Width = 5, Height = 5, Fill = Brushes.Red, RenderTransform = new TranslateTransform() { X = -2, Y = -2 } };
                    Ellipse ellipse2 = new Ellipse() { Width = 5, Height = 5, Fill = Brushes.Red, RenderTransform = new TranslateTransform() { X = -2, Y = -2 } };
    
                    ellipse1.SetBinding(Canvas.LeftProperty, new Binding("X1") { Source = newline, Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
                    ellipse1.SetBinding(Canvas.TopProperty, new Binding("Y1") { Source = newline, Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
                    ellipse2.SetBinding(Canvas.LeftProperty, new Binding("X2") { Source = newline, Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
                    ellipse2.SetBinding(Canvas.TopProperty, new Binding("Y2") { Source = newline, Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
                    canvas.Children.Add(ellipse1);
                    canvas.Children.Add(ellipse2);
    
    
                    _lines.Add(newline);
                    this.marker1.DataContext = newline;
                    this.marker2.DataContext = newline;
    
                    Dispatcher.BeginInvoke(new Action(() => { marker2.RaiseEvent(e); }), System.Windows.Threading.DispatcherPriority.Render);
    
                }
            }
    
    
    
            private void Line_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
            {
                marker1.Visibility = Visibility.Hidden;
                marker2.Visibility = Visibility.Hidden;
            }
    
     
        }
    }

    后台wpf调用:

      dragLine = new DragLine();
                dragLine.DragPoints= new List<Point>()
                {
                new Point(400,10),
                new Point(90,180),
                new Point (100,130),
                new Point (170,200),
                new Point(300,80),
                new Point (150,300)
                };
                this.grid1.Children.Add(dragLine);

  • 相关阅读:
    [转]Navicat Premium 12试用期的破解方法
    Redis禁用持久化功能的设置
    阿里云ECS安装的redis服务器,用java代码去连接报错。
    关于Jedis连接Linux上的redis出现 DENIED Redis is running in protected mode问题的解决方案
    修改了jdk在环境变量中的路径怎么cmd中的jdk版本没有变
    阿里云上部署tomcat启动后,通过http不能访问
    【终结篇】不要再问我程序员该如何提高了……
    我是怎么把一个项目带崩的
    eterm和easyfare的官网地址
    java UTC时间和local时间相互转换
  • 原文地址:https://www.cnblogs.com/dangnianxiaoqingxin/p/13210276.html
Copyright © 2020-2023  润新知