首页 > 其他分享 >WPF canvas locate

WPF canvas locate

时间:2024-07-13 21:31:39浏览次数:14  
标签:locate canvas elp markedElp System new using WPF public

//xaml
<Window x:Class="WpfApp210.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:WpfApp210"
        mc:Ignorable="d" WindowState="Maximized"
        Title="{Binding LocatedPt,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
        Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <DataGrid Grid.Column="0" x:Name="dg" 
            ItemsSource="{Binding BooksList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
            SelectionChanged="dg_SelectionChanged" />
        <Canvas Grid.Column="1" x:Name="cvs" ClipToBounds="True"/>
    </Grid>
</Window>


//cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
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 WpfApp210
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            
            this.Loaded += MainWindow_Loaded;
            this.DataContext = this;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler?.Invoke(this, new PropertyChangedEventArgs(propName));
            }
        }

        private ObservableCollection<Book> booksList;
        public ObservableCollection<Book> BooksList
        {
            get
            {
                return booksList;
            }
            set
            {
                if (value != booksList)
                {
                    booksList = value;
                    OnPropertyChanged(nameof(BooksList));
                }
            }
        }

        private string locatedPt;
        public string LocatedPt
        {
            get
            {
                return locatedPt;
            }
            set
            {
                if(value!= locatedPt) 
                {
                    locatedPt = value;
                    OnPropertyChanged(nameof(LocatedPt));
                }
            }
        }

        Ellipse markedElp { get; set; }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            double width = this.ActualWidth-200;
            double height = this.ActualHeight;
            Random rnd = new Random();
            BooksList = new ObservableCollection<Book>();
            for (int i = 0; i < 200; i++)
            {
                int ptX = rnd.Next(0, (int)width - 30);
                int ptY = rnd.Next(0, (int)height - 30);

                BooksList.Add(new Book()
                {
                    Id = i + 1,
                    Name = $"Name_{i + 1}",
                    Pt = new Point(ptX, ptY)
                });
                Ellipse elp = new Ellipse();
                elp.Width = 30;
                elp.Height = 30;
                elp.StrokeThickness = 5;
                elp.Stroke = new SolidColorBrush(Colors.Cyan);
                //elp.Fill = new SolidColorBrush(Colors.Cyan);
                Canvas.SetLeft(elp, ptX);
                Canvas.SetTop(elp, ptY);
                cvs.Children.Add(elp);
            }

            markedElp = new Ellipse();
            markedElp.Width = 50;
            markedElp.Height = 50;
            markedElp.Stroke = new SolidColorBrush(Colors.Red);
            markedElp.StrokeThickness = 15;
            markedElp.Fill = new SolidColorBrush(Colors.Transparent);
        }

        private void DrawEllipses()
        {

        }

        private void dg_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(dg.SelectedItem!=null)
            {
                var bk=dg.SelectedItem as Book;
                if(bk!=null)
                { 
                    Canvas.SetLeft(markedElp, bk.Pt.X-10);
                    Canvas.SetTop(markedElp, bk.Pt.Y-10);
                    if(cvs.Children.Contains(markedElp))
                    {
                        cvs.Children.Remove(markedElp);
                    }
                    cvs.Children.Add(markedElp);
                    LocatedPt = $"({bk.Pt.X},{bk.Pt.Y})";
                }
            }
        }
    }

    public class Book
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public Point Pt { get; set; }
    }
}

 

 

 

 

 

 

 

 

标签:locate,canvas,elp,markedElp,System,new,using,WPF,public
From: https://www.cnblogs.com/Fred1987/p/18300742

相关文章

  • 在WPF中使用WriteableBitmap对接工业相机及常用操作
    写作背景写这篇文章主要是因为工业相机(海康、大恒等)提供的.NET开发文档和示例程序都是用WinForm项目来说明举例的,而在WPF项目中对图像的使用和处理与在WinForm项目中有很大不同。在WinForm中用System.Drawing.Bitmap来处理图像,而在WPF中是用System.Windows.Media.Imaging.Writeab......
  • WPF中style的应用(小白快速上手)
    1.解释说明    -通过设置style资源词典可以批量设置控件,不仅节省大量时间,还能方便统一修改    -重复利用Border这个控件,可以自由设计新的控件风格    -这里要注意,虽然style也是写在xaml文件中,但是其文件类型为资源词典类型,这里程序示例也进行不......
  • 优质WPF免费学习资源分享(含代码)
    WPF自学资源分享背景自身是winform开发,winform岗位比较少。wpf和winfom殊途同归,所以自身最近也在学习wpf。分享一下自己寻找到的wpf学习资源,希望对大家有帮助。学习资源推荐书籍学习资源《深入简出WPF》-刘铁猛作者是微软的高级开发工程师,多年的wpf开发经验。不过作者......
  • WPF开源的一款免费、开箱即用的翻译、OCR工具
    思维导航前言WPF介绍工具快捷键工具下载工具部分功能截图项目源码地址优秀项目和框架精选DotNetGuide技术社区交流群前言今天大姚给大家分享一款由WPF开源的、免费的(MITLicense)、即开即用、即用即走的翻译、OCR工具:STranslate。WPF介绍WPF是一个强大的桌面应......
  • 【WPF控件样式】
    自定义弹窗单选框CheckBox扁平化<Stylex:Key="CheckBoxStyle1"TargetType="{x:TypeCheckBox}"><SetterProperty="BorderThickness"Value="1"/><SetterProperty="Template"><Setter.V......
  • WPF中引用不到相对路径图片?
    在wpf中使用相对路径运行项目时却不显示图片怎么解决?新建img文件夹添加所需要的图片选中图片右键属性设置属性重新生成即可运行效果转载请标明出处!......
  • WPF WebBrowser navigate to website via url and escape script error warning
    Copyfrom https://www.iditect.com/faq/csharp/wpf-webbrowser-control--how-to-suppress-script-errors.html#:~:text=To%20suppress%20these%20script%20errors%2C%20you%20can%20handle,using%20the%20Cancel%20property%20of%20the%20WebBrowserNavigatingEventArgs%20pa......
  • WPF display and host pdf via WebBrowser
    //xaml<Windowx:Class="WpfApp206.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • WPF WebBrowser make sure the path or Internet address is correct
      Onepossiblecauseisincludechinesecharacters, //WrongcodeprivatevoidOpenClick(objectsender,RoutedEventArgse){OpenFileDialogdialog=newOpenFileDialog();dialog.Filter="PDFFiles|*.pdf|AllFiles|*.*";i......
  • Simple WPF: S3实现MINIO大文件上传并显示上传进度
    最新内容优先发布于个人博客:小虎技术分享站,随后逐步搬运到博客园。创作不易,如果觉得有用请在Github上为博主点亮一颗小星星吧!目的早两天写了一篇S3简单上传文件的小工具,知乎上看到了一个问题问如何实现显示MINIO上传进度,因此拓展一下这个小工具能够在上传大文件时显示进度。完......