c# – WPF DataGrid:你如何获得单个单元格的内容?

c# – WPF DataGrid:你如何获得单个单元格的内容?,第1张

概述如何在C#中获取 WPF工具包DataGrid的单个单元格的内容? 根据内容,我的意思是可能存在的一些纯文本. 按照Phillip的说法 – DataGrid通常是数据绑定的.下面是我的WPF DataGrid绑定到ObservableCollection< PersonName>的示例.其中PersonName由FirstName和LastName(两个字符串)组成. DataGrid支持自动创 如何在C#中获取 WPF工具包DataGrID的单个单元格的内容?

根据内容,我的意思是可能存在的一些纯文本.

@R_419_6120@ 按照Phillip的说法 – DataGrID通常是数据绑定的.下面是我的WPF DataGrID绑定到ObservableCollection< Personname>的示例.其中Personname由Firstname和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; }        }    }}
总结

以上是内存溢出为你收集整理的c# – WPF DataGrid:你如何获得单个单元格的内容?全部内容,希望文章能够帮你解决c# – WPF DataGrid:你如何获得单个单元格的内容?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1248562.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存