首页 > 其他分享 >WPF datagrid scrolldown and change the marked the location in canvas

WPF datagrid scrolldown and change the marked the location in canvas

时间:2024-06-04 22:12:55浏览次数:20  
标签:canvas markedElp System marked datagrid Windows new using elp

<Window x:Class="WpfApp134.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:WpfApp134"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <DataGrid x:Name="dg" Grid.Column="0" SelectionChanged="dg_SelectionChanged" />
        <Canvas x:Name="cvs" Grid.Column="1"/>
    </Grid>
</Window>


//cs
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
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.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp134
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Ellipse markedElp { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
            markedElp = new Ellipse();
            markedElp.Width = 30;
            markedElp.Height = 30;
            markedElp.Fill = new SolidColorBrush(Colors.Red);
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            double width = this.ActualWidth;
            double height= this.ActualHeight;
            Random rnd = new Random();
            List<EllipseModel> modelsList = new List<EllipseModel>();
            for (int i = 0; i < 400000; i++)
            {
                Ellipse elp = new Ellipse();
                elp.Width = 5;
                elp.Height = 5;
                elp.Stroke=new SolidColorBrush(Colors.Blue);
                double x= rnd.Next(0,(int)width);
                double y= rnd.Next(0,(int)height);
                Canvas.SetLeft(elp,x);
                Canvas.SetTop(elp,y);
                cvs.Children.Add(elp);
                EllipseModel model = new EllipseModel();
                model.Id = i + 1;
                model.X = x;
                model.Y = y;
                modelsList.Add(model);
            }
            dg.ItemsSource= modelsList;
        }

        private void dg_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Application.Current.Dispatcher.BeginInvoke(new Action(() =>
            {
               if(e!=null && e.AddedItems!=null && e.AddedItems.Count>0)
                {
                    var temp = e.AddedItems[0] as EllipseModel;
                    if(temp!=null)
                    {
                        if(cvs.Children.Contains(markedElp))
                        {
                            cvs.Children.Remove(markedElp);
                        }
                        Canvas.SetLeft(markedElp,temp.X);
                        Canvas.SetTop(markedElp, temp.Y);
                        cvs.Children.Add(markedElp);
                        this.Title = $"X:{temp.X},Y:{temp.Y}";
                    }
                } 
            }));
        }
    }

    class EllipseModel
    {
        public int Id { get; set; }
        public double X { get; set; }
        public double Y { get; set; }
    }
}

 

 

 

标签:canvas,markedElp,System,marked,datagrid,Windows,new,using,elp
From: https://www.cnblogs.com/Fred1987/p/18231877

相关文章

  • View->Canvas使用RectF方式绘制Bitmap,RectF大小和Bitmap大小不同导致绘制效果不同
    图片XML文件<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent......
  • canvas的基础使用
    目录1.了解canvas1.1canvas元素1.2兼容性1.3支持性2.使用canvas2.1获取canvas绘图上下文2.2绘制demo3.绘制矩形4.绘制path4.1绘制线条lineTo4.2绘制圆arc5.设置线条宽度、颜色、连接点样式等5.1线条宽度lineWidth5.2颜色fillStyle、strokeStyle5.3连接......
  • WPF canvas mousewheel to zoom in or out
    <Windowx:Class="WpfApp133.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • WPF DataGrid自动增长序号列
    ///<summary>///自动增长序号列///</summary>publicclassDataGridRowIndexColumn:DataGridTextColumn{///<summary>///可以指定开始序号///</summary>publicintStartIndex{get{return(int)GetValue(StartIndex......
  • WebGPU学习(11)--- 独立于 Canvas 绘制
    更多精彩内容尽在数字孪生平台,关注公众号:sky的数孪技术,技术交流、源码下载请添加VX:digital_twin123只想创建图像数据而不使用Canvas当使用WebGL时,我们始终必须从Canvas获取WebGL渲染上下文,因为渲染上下文与绘图目标紧密相关。但是WebGPU不一定需要Canvas来......
  • canvas 鼠标与点之间连线
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"/><metaname="viewport"content="width=device-width,user-scalable=no,initial-scale=1.0"/><title>Do......
  • canvas 合并图片和文字
    代码asyncgetImgInfo(img,text){returnnewPromise((resolve,reject)=>{constcanvas=document.createElement("canvas");canvas.width=52;canvas.height=68;constctx=canvas.getContext("2d");......
  • Avalonia下DataGrid多选MVVM绑定的功能
    安装Avalonia.Xaml.BehaviorsInstall-PackageAvalonia.Xaml.BehaviorsDataGridSelectedItemsBehavior.csusingAvalonia;usingAvalonia.Controls;usingAvalonia.Threading;usingAvalonia.Xaml.Interactivity;namespaceCgdataBase;publicclassDataGridSelected......
  • 通过画布(Canvas)实现 ZLMRTCClient 同一视频流多次显示时只拉取一次
    效果预览视频画面网络请求代码实现ZLMRTCClient.js首先需要修改ZLMRTCClient.js的代码,解决由于网络导致播放失败时无法触发WEBRTC_OFFER_ANWSER_EXCHANGE_FAILED事件的问题。修改前:修改后:修改内容://添加catch()axios({}).then(()=>{}).catch(()=>{......
  • 使用 Marked 2 为 CotEditor 预览 markdown 内容
    CotEditor虽然各种好,但毕竟功能比较轻量,用来编辑markdown甚至没有一个预览功能。不过好在它还支持脚本拓展。我之前曾经用过coteditor_markdown_setPublic仓库里的预览脚本。不过这个脚本的预览依赖Safari实现,而且颇有一些年久失修,很多Markdown特性都不支持。最近突然想......