WPF datagrid scrolldown and change the marked the location in canvas

<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; }
    }
}

 

 

 

posted @ 2024-06-04 22:02  FredGrit  阅读(3)  评论(0编辑  收藏  举报