gpt4 book ai didi

c# - "Drag and Copy"用于 WPF 数据网格

转载 作者:行者123 更新时间:2023-11-30 17:32:47 25 4
gpt4 key购买 nike

我的工作场所正在开发一个用于构建通用公司电子表格的内部程序。我们的程序不需要过多的功能,但我们想要包括的一件事是能够单击一个单元格,向上/向下/向左/向右拖动它,并将原始单元格的内容复制到用户像 Excel 一样选择的单元格。

我对可靠、明确且符合上下文的答案的搜索并未取得成果。我最接近的发现是 this SO question以及关于在数据网格上移动一行的物理位置的文章。该问题的作者没有成功,报告说他们完全跳过了“拖动和复制”的实现。

在遵循 MVVM 构建的应用程序中是否有合理的方法来实现此功能?

最佳答案

简单 XAML - 注意添加到数据网格的 SelectionUnit、SelectedCellsChanges 和 Keyup 事件

    <Window x:Class="WpfApp4.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:WpfApp4"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid x:Name="MyGrid" HorizontalAlignment="Left" Height="276" Margin="18,21,0,0" VerticalAlignment="Top" Width="466" SelectionUnit="Cell" SelectedCellsChanged="SelectionChanged" KeyUp="MyGrid_PreviewKeyUp"/>
</Grid>
</Window>

一些简单的 C# 代码:

    using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WpfApp4
{
public class MyItems
{
public int Col1 { get; set; }
public int Col2 { get; set; }
public int Col3 { get; set; }
public int Col4 { get; set; }
}

public partial class MainWindow : Window
{
// create a source for the datagrid
public List<MyItems> DataList { get; set; }

// somewhere to hold the selected cells
IList<DataGridCellInfo> DataGridSelectedCells { get; set; }

public MainWindow()
{
InitializeComponent();
DataContext = this;

DataList = new List<MyItems>()
{
new MyItems() { Col1=1, Col2=2, Col3=3, Col4=4},
new MyItems() { Col1=5, Col2=6, Col3=7, Col4=8},
new MyItems() { Col1=9, Col2=10, Col3=11, Col4=12},
new MyItems() { Col1=13, Col2=14, Col3=15, Col4=16},
};

MyGrid.ItemsSource = DataList;

}

private void SelectionChanged(object sender, SelectedCellsChangedEventArgs e)
{
DataGridSelectedCells = MyGrid.SelectedCells;
}

private void MyGrid_PreviewKeyUp(object sender, KeyEventArgs e)
{
// Check your key here (Ctrl D, Ctrl R etc)
// then loop around your data looking at what is selected
// chosing the direction based on what key was pressed
foreach (DataGridCellInfo d in DataGridSelectedCells)
{ // get the content of the cell
var cellContent = d.Column.GetCellContent(d.Item);
if (cellContent != null)
{ // if it's not null try to get the content
DataGridCell dc = (DataGridCell)cellContent.Parent;
TextBlock tb = (TextBlock)dc.Content;

// Change the contents of tb.Content here
// or dump for debugging
Console.WriteLine(tb.Text);
}
}
}
}
}

用户可以向任何方向拖动单元格,“GridSelectedCells”将只填充选定的单元格。使用 KeyUp(或其他首选)事件允许用户复制(或使用上下文菜单实现右键单击事件),然后根据需要循环遍历数据(向前或向后)以进行复制。

虽然这不是一个完整的解决方案,但应该可以帮助您入门。

关于c# - "Drag and Copy"用于 WPF 数据网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45920251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com