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

发布于 2024-08-02 01:30:12 字数 77 浏览 4 评论 0原文

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏末 2024-08-09 01:30:12

按照 Phillip 的说法 - DataGrid 通常是数据绑定的。 下面是一个示例,其中我的 WPF DataGrid 绑定到 ObservableCollection,其中 PersonNameFirstName 组成code> 和 LastName (都是字符串)。

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

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

The DataGrid supports 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.

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; }
        }
    }
}
ゞ花落谁相伴 2024-08-09 01:30:12

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

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

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.

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 tree to find the control (or controls) that is rendered on screen? How do you expect to reference the cell, by row and column index?

花海 2024-08-09 01:30:12

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

DataRowView rowView = e.Row.Item as DataRowView;

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

DataRowView rowView = e.Row.Item as DataRowView;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文