效果图如下:

viewmodel


using PropertyChanged;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;using System.Collections.ObjectModel;
using System.Windows.Controls;namespace _1_5GraphicsWpf.ViewModel
{//颜色选择列表 数据模板[AddINotifyPropertyChangedInterface]public class VMColor : INotifyPropertyChanged{public event PropertyChangedEventHandler PropertyChanged;private int id;public int ID{get;set;}private string pcolor;public string PColor{get;set;}}//样式选择列表 数据模板[AddINotifyPropertyChangedInterface]public class VMStyle : INotifyPropertyChanged{public event PropertyChangedEventHandler PropertyChanged;private int id;public int ID{get ;set;}private string pstyle;public string PStyle{get;set;}}//一些全局变量设置 数据模板[AddINotifyPropertyChangedInterface]public class Global_Window1 : INotifyPropertyChanged{public event PropertyChangedEventHandler PropertyChanged;//int Combo_ColorIndex;//int Combo_StyleIndex;//private int combo_ColorIndex;public int Combo_ColorIndex{get ;set;}//private int combo_StyleIndex;public int Combo_StyleIndex{get ;set;}//private double thickness;public double Thickness{get ;set;}//private Collection<ValidationRule> datavalidationRules;//public Collection<ValidationRule> dataValidationRules { get; set; }//private Collection<ValidationRule> rangevalidationRules;//public Collection<ValidationRule> rangeValidationRules { get; set; }}
}

数据绑定中的数据校验


using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;namespace _1_5GraphicsWpf.ControlValidationRule
{public class RangeValidationRule : ValidationRule{private int min=0 ;public int Min{get { return min ; }set { min= value; }}private int max = 100;public int Max {get { return max; }set { max  = value; }}public RangeValidationRule() : base() { }//public RangeValidationRule(int a ,int b ):base()//{//    min = a;//    max = b;//}public override ValidationResult Validate(object value, CultureInfo cultureInfo){double d = 0;if (double.TryParse(value.ToString(),out d)){if (d>=min&&d<=max){return new ValidationResult(true,null);}}return new ValidationResult(false,"数据范围验证失败");}}public class DataValidationRule : ValidationRule{public override ValidationResult Validate(object value, CultureInfo cultureInfo){double d = 0;if (double.TryParse(value.ToString(), out d)){return new ValidationResult(true, null);}return new ValidationResult(false, "内容无法转化为小数");}}}

windows1.xaml.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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.Shapes;
using _1_5GraphicsWpf.ViewModel;
using _1_5GraphicsWpf.ControlValidationRule;namespace _1_5GraphicsWpf
{public partial class Window1 : Window{public Window1(){InitializeComponent();Init();}ObservableCollection<VMColor> ColorCollection;ObservableCollection<VMStyle> StyleCollection;Global_Window1 GlobalVaries;void Init(){ColorCollection = new ObservableCollection<VMColor> {new VMColor{ID = 0,PColor="默认"},new VMColor{ID = 1,PColor="红色"},new VMColor{ID = 2,PColor="绿色" }};StyleCollection = new ObservableCollection<VMStyle> {new VMStyle{ID = 0,PStyle="平坦"},new VMStyle{ID = 1,PStyle="直角"},new VMStyle{ID = 2,PStyle="三角"},new VMStyle{ID = 3,PStyle="圆形"}};GlobalVaries = new Global_Window1() {Combo_ColorIndex = 0,Combo_StyleIndex = 0,Thickness = 1};this.ComboColor.ItemsSource = ColorCollection;this.ComboStyle.ItemsSource = StyleCollection; this.DataContext = GlobalVaries;this.textBoxThick.AddHandler(Validation.ErrorEvent,new RoutedEventHandler(this.ValidationError));}void ValidationError(object sender,RoutedEventArgs e){TextBox obj = sender as TextBox;if (Validation.GetErrors(obj).Count>0){obj.ToolTip = Validation.GetErrors(this.textBoxThick)[0].ErrorContent.ToString();}}Point startPoint;List<Point> pointList = new List<Point>();private void Canvas_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e){startPoint = e.GetPosition(myCanvas);}private void Canvas_PreviewMouseMove(object sender, MouseEventArgs e){if (e.LeftButton == MouseButtonState.Pressed){// 返回指针相对于Canvas的位置Point point = e.GetPosition(myCanvas);if (pointList.Count == 0){// 加入起始点pointList.Add(new Point(this.startPoint.X, this.startPoint.Y));}else{// 加入移动过程中的point  颜色pointList.Add(point);}// 去重复点var disList = pointList.Distinct().ToList();var count = disList.Count(); // 总点数if (point != this.startPoint && this.startPoint != null){var l = new Line();//string color = (ComboColor.SelectedItem as ComboBoxItem).Content as string;int color = int.Parse((ComboColor.SelectedValue as VMColor).ID.ToString());switch (color){case 0:l.Stroke = Brushes.Black;break;case 1:l.Stroke = Brushes.Red;break;case 2:l.Stroke = Brushes.Green;break;default:l.Stroke = Brushes.Black;break;}int style = int.Parse((ComboStyle.SelectedValue as VMStyle).ID.ToString());switch (style){case 0:l.StrokeDashCap = PenLineCap.Flat;l.StrokeStartLineCap = PenLineCap.Flat;l.StrokeEndLineCap = PenLineCap.Flat;break;case 1:l.StrokeDashCap = PenLineCap.Square;l.StrokeStartLineCap = PenLineCap.Square;l.StrokeEndLineCap = PenLineCap.Square;break;case 2:l.StrokeDashCap = PenLineCap.Triangle;l.StrokeStartLineCap = PenLineCap.Triangle;l.StrokeEndLineCap = PenLineCap.Triangle;break;case 3:l.StrokeDashCap = PenLineCap.Round;l.StrokeStartLineCap = PenLineCap.Round;l.StrokeEndLineCap = PenLineCap.Round;break;default:l.StrokeDashCap = PenLineCap.Round;l.StrokeStartLineCap = PenLineCap.Round;l.StrokeEndLineCap = PenLineCap.Round;break;}//l.StrokeLineJoin = PenLineJoin.Round;//l.StrokeStartLineCap = PenLineCap.Triangle;l.StrokeThickness = this.GlobalVaries.Thickness ;if (count < 2)return;l.X1 = disList[count - 2].X;  // count-2  保证 line的起始点为点集合中的倒数第二个点。l.Y1 = disList[count - 2].Y;// 终点X,Y 为当前point的X,Y l.X2 = point.X;l.Y2 = point.Y;myCanvas.Children.Add(l);}}}private void MyCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e){pointList.Clear();}private void Button_Click(object sender, RoutedEventArgs e){this.myCanvas.Children.Clear();//清除pointList.Clear();}}
}

windows1.xaml

<Window x:Class="_1_5GraphicsWpf.Window1"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:_1_5GraphicsWpf"xmlns:cvr="clr-namespace:_1_5GraphicsWpf.ControlValidationRule"mc:Ignorable="d"Title="Window1" Height="450" Width="800"><Grid><DockPanel  Background="AliceBlue"><Grid><Grid.ColumnDefinitions><ColumnDefinition Width="5*"></ColumnDefinition><ColumnDefinition Width="13*"></ColumnDefinition></Grid.ColumnDefinitions><Canvas Grid.Column="1" x:Name="myCanvas"   DockPanel.Dock="Right" Background="LightGray" PreviewMouseLeftButtonDown="Canvas_PreviewMouseLeftButtonDown" PreviewMouseMove="Canvas_PreviewMouseMove"MouseLeftButtonUp="MyCanvas_MouseLeftButtonUp" ></Canvas><Grid DockPanel.Dock="Left" x:Name="LeftDockPanel"><Grid.ColumnDefinitions><ColumnDefinition Width="*"></ColumnDefinition><ColumnDefinition Width="3*"></ColumnDefinition></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition Height="30px"></RowDefinition><RowDefinition Height="30px"></RowDefinition><RowDefinition Height="30px"></RowDefinition><RowDefinition Height="30px"></RowDefinition><RowDefinition Height="30px"></RowDefinition><RowDefinition Height="30px"></RowDefinition><RowDefinition Height="30px"></RowDefinition></Grid.RowDefinitions><Label Grid.Column="0" Grid.Row="0">颜色œ˜œ˜œ˜œ˜</Label><ComboBox x:Name="ComboColor" Grid.Row="0" Grid.Column="1" Margin="6 3"SelectedValuePath="{Binding Path=ID}" SelectedIndex="{Binding Path=Combo_ColorIndex}" ><ComboBox.ItemTemplate><DataTemplate><StackPanel Orientation="Horizontal"><TextBlock Text="{Binding Path=PColor}"></TextBlock></StackPanel></DataTemplate></ComboBox.ItemTemplate></ComboBox><Label Grid.Column="0" Grid.Row="1">线条粗细œ˜œ˜œ˜œ˜</Label><TextBox x:Name="textBoxThick" Grid.Row="1"  Grid.Column="1" Margin="6,3" ><TextBox.Text><Binding  Path="Thickness" NotifyOnValidationError="True" NotifyOnSourceUpdated="True" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"><Binding.ValidationRules><cvr:DataValidationRule ValidatesOnTargetUpdated="True"></cvr:DataValidationRule></Binding.ValidationRules></Binding></TextBox.Text></TextBox><Slider Minimum="0" Maximum="100" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2" Value="{Binding Path=Thickness, Mode=TwoWay}"></Slider><Label Grid.Column="0" Grid.Row="3">样式œ˜œ˜œ˜œ˜</Label><ComboBox x:Name="ComboStyle" Grid.Row="3" Grid.Column="1" Margin="6 3"SelectedValuePath="{Binding Path=ID}" SelectedIndex="{Binding Path=Combo_StyleIndex}" ><ComboBox.ItemTemplate><DataTemplate><StackPanel Orientation="Horizontal"><TextBlock Text="{Binding Path=PStyle}"></TextBlock></StackPanel></DataTemplate></ComboBox.ItemTemplate></ComboBox><Button Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" Margin="10 3" Content="清除" Click="Button_Click"></Button></Grid></Grid></DockPanel></Grid>
</Window>

wpf实现简易画板功能相关推荐

  1. wpf实现简易画板功能(带截取画板,签名截图等等)

    参照自:https://www.cnblogs.com/jixin/p/4730170.html https://blog.csdn.net/u012408847/article/details/86 ...

  2. c#创建画布_c# GDI+简单绘图(四) 简易画板功能

    前几篇我已经向大家介绍了如何使用GDI+来绘图,并做了一个截图的实例,这篇我向大家介绍下如何来做一个类似windows画图的工具. 个人认为如果想做一个功能强大的绘图工具,那么单纯掌握GDI还远远不够 ...

  3. 用java编写一个简易功能画板_用Java语言编写一个简易画板

    讲了三篇概博客的概念,今天,我们来一点实际的东西.我们来探讨一下如何用Java语言,编写一块简易的画图板. 一.需求分析 无论我们使用什么语言,去编写一个什么样的项目,我们的第一步,总是去分析这个项目 ...

  4. cefsharp 发送请求服务器_使用 WPF 版简易 SIP 服务器向 GB28181 摄像头发送直播请求...

    使用 WPF 版简易 SIP 服务器向 GB28181 摄像头发送直播请求 目录 一.引言 二.项目渊源 三.软件使用及 SIP INVITE 流程 (一) 注册和心跳 (二) 直播 INVITE 四 ...

  5. 基于QGraphicsView的简易画板EasyCanvas -- 第一版

    最近使用 QGraphicsView 做了个简易的画板 EasyCanvas ,界面效果如下: 具有如下功能: 画布设置 图元添加和修改 保存为图片 第二版已更新 基于QGraphicsView的简易 ...

  6. Qt Creator实现简易画板代码解析【工具栏】【画板】

    演示效果 工具栏通常位于菜单栏的下方,上面存放着一些小按钮,如下图所示. 以下所有功能都是直接通过代码实现,而不是在设计模式下ui界面通过拖拽实现.当然,它是可以用拖拽实现的. 引入图片资源 图片资源 ...

  7. react实现简易画板程序

    react实现简易画板程序 文章同时发布于:https://pengfeixc.com/blog/60d073bce97367196dce3efc. 在这之前,我写过一个vue版本的画板程序.最近因为 ...

  8. Java图形化设计 简易画板

    简易画板 UI界面设置 监听器类 图形保存 github源代码下载: 画图工具源代码. UI界面设置 根据我们想实现的画板要求,可以分步骤进行解决.在创建画板之前,首先创建窗体框架,利用java提供的 ...

  9. java 2d划线 刷子_简易画板的JAVA实现

    要实现一个画图板,首先我们要掌握的是图形界面开发和事件监听机制.在Java中,图形界面开发有三套组件: 1.AWT组件:这是JDK1.0推出的图形组件类,位于java.awt这个包下.AWT组件被称为 ...

最新文章

  1. LEB(Leading edge blanking)是什么
  2. 大话设计模式(十四 设计模式不能戏说!设计模式怎就不能戏说?)
  3. 数字化时代,CIO该如何理解数字能力
  4. hdu 4495(hash+二分+dp)
  5. Java使用预定格式获取时间字符串
  6. 深入学习jQuery的三种常见动画效果
  7. Java线程同步的一些例子
  8. Linus系统下查看系统版本
  9. ssl证书的生成与签名
  10. 深度学习掩膜_学习资源 | NOAA连接AI与环境科学(九)—海洋环境视频和图像分析教程...
  11. powell法-鲍威尔法详解-附案例matlab代码
  12. 解决checkra1n越狱失败及越狱后AFC2失效问题
  13. 多开助手完美版,APP一键多开,支持安卓10
  14. 稳压电源: 电路图及类型
  15. [读书笔记]Spring中的容器设计详解
  16. 北大邮箱收件服务器,邮箱手机客户端设置说明
  17. 多重共线性分析 与 方差膨胀因子VIF
  18. 杰迷福利!1句python命令下载Jay Chou全部专辑MV
  19. 微软展示Windows 8新LOGO:倾斜纯蓝色窗口
  20. PDF如何旋转其中一页?

热门文章

  1. printf()详解之终极无惑
  2. 记录一次日志采集工具fluentd踩坑
  3. Laradock入门到精通
  4. [C语言]栈和队列的输入输出
  5. Protobuf 中的 timestamp 与 Go time 的转换
  6. 爬山搜索法c语言代码,模拟退火算法和爬山算法 – 算法大全
  7. 高级程序员如何面对职场压力?--明确目标赢在职场
  8. 捷图书排行Top 20
  9. vim插件管理器:Vundle的安装
  10. KITL是怎么样工作的?