C# WPF DataGrid:如何获取单个单元格的内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1295843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:48:31  来源:igfitidea点击:

WPF DataGrid: How do you get the content of a single cell?

c#wpfdatagridwpftoolkit

提问by Partial

How do you get the content of a single cell of a WPF toolkit DataGrid in C#?

你如何在C#中获取WPF工具包DataGrid的单个单元格的内容?

By content I mean some plain text that could be in there.

我所说的内容是指可能存在于其中的一些纯文本。

采纳答案by Rob Sobers

Following what Phillip said - the DataGridis usually data-bound. Below is an example where my WPF DataGridis bound to an ObservableCollection<PersonName>where a PersonNameis comprised of a FirstNameand LastName(both strings).

按照菲利普所说的 -DataGrid通常是数据绑定的。下面是一个示例,其中我的 WPFDataGrid绑定到ObservableCollection<PersonName>aPersonName由 aFirstNameLastName(两个字符串)组成。

The DataGridsupports automatic column creation so the example is quite simple. You'll see that I can access rows by their index and get the value of a cell in that row by using the property name that corresponds to the column name.

DataGrid支持自动列创建这样的例子很简单。您将看到我可以通过索引访问行,并通过使用与列名对应的属性名获取该行中单元格的值。

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            // Create a new collection of 4 names.
            NameList n = new NameList();

            // Bind the grid to the list of names.
            dataGrid1.ItemsSource = n;

            // Get the first person by its row index.
            PersonName firstPerson = (PersonName) dataGrid1.Items.GetItemAt(0);

            // Access the columns using property names.
            Debug.WriteLine(firstPerson.FirstName);

        }
    }

    public class NameList : ObservableCollection<PersonName>
    {
        public NameList() : base()
        {
            Add(new PersonName("Willa", "Cather"));
            Add(new PersonName("Isak", "Dinesen"));
            Add(new PersonName("Victor", "Hugo"));
            Add(new PersonName("Jules", "Verne"));
        }
    }

    public class PersonName
    {
        private string firstName;
        private string lastName;

        public PersonName(string first, string last)
        {
            this.firstName = first;
            this.lastName = last;
        }

        public string FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }

        public string LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }
    }
}

回答by Philipp Schmid

Normally, the content of a DataGrid cell is data-bound, and therefore reflect the state of a property (in most cases) of an object which is being displayed in a given row. Hence it might be easier to access the model rather than the view.

通常,DataGrid 单元格的内容是数据绑定的,因此反映了在给定行中显示的对象的属性状态(在大多数情况下)。因此,访问模型而不是视图可能更容易。

Having said that (access model not view) my question is: what are you trying to do? Are you looking of ways to traverse the visual treeto find the control (or controls) that is rendered on screen? How do you expect to reference the cell, by row and column index?

话虽如此(访问模型不是视图)我的问题是:你想做什么?您是否正在寻找遍历可视化树以查找呈现在屏幕上的控件(或多个控件)的方法?您希望如何通过行和列索引引用单元格?

回答by Oliver

If you bind using a DataTable you can get the DataRowView from the Row's Item property.

如果使用 DataTable 进行绑定,则可以从 Row 的 Item 属性中获取 DataRowView。

DataRowView rowView = e.Row.Item as DataRowView;