【问题标题】:WPF DataGrid: How do you get the content of a single cell?WPF DataGrid:如何获取单个单元格的内容?
【发布时间】:2010-11-20 17:13:27
【问题描述】:

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

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

【问题讨论】:

  • 我知道这对某些人来说可能听起来很简单,但我是 WPF 的新手,并且一直在尝试使用 DataGridView 在 Forms 中做一些类似的事情,但没有做任何类似的事情。因此,非常感谢您提供详细的解决方案!
  • 也许事情没那么简单……
  • 我想我会在 WindowsFormsIntegration 的帮助下使用 Forms 中的 DataGridView...

标签: c# wpf datagrid wpftoolkit


【解决方案1】:

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

DataRowView rowView = e.Row.Item as DataRowView;

【讨论】:

    【解决方案2】:

    按照 Phillip 所说的 - DataGrid 通常是数据绑定的。下面是我的 WPF DataGrid 绑定到 ObservableCollection<PersonName> 的示例,其中 PersonNameFirstNameLastName(两个字符串)组成。

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

    【讨论】:

    • 谢谢!我知道必须有某种方法...比 Forms 中的 DataGridView 稍微复杂一些,但 WPF 不如 Forms 成熟。
    • 没问题。我认为 Xceed(第三方)DataGrid 使用 DataGridView 构造。
    • public string FirstName { get; set; } public string LastName { get; set; } 转换为自动属性 ​​:D
    【解决方案3】:

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

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

    【讨论】:

    • 例如,使用 Forms DataGridView 您可以执行以下操作: string cellContent = dataGridView1.Rows[0].Cells[1].ToString();
    • 如何使用 WPF 数据网格做类似的事情?
    • 是的,我希望能够按行和列索引获取单元格。
    • 我也在寻找这样的东西。一个很好的例子是,在 DataGridView 中,您可以通过这种方式进行通用复制和粘贴,但这在 WPF DataGrid 中似乎是不可能的。是否有人知道这是否可能在 WPF DataGrid 的下一版本中实现?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 2011-01-31
    相关资源
    最近更新 更多