【问题标题】:WPF: How to get cell value from DataGridRowWPF:如何从 DataGridRow 获取单元格值
【发布时间】:2018-01-25 05:58:27
【问题描述】:

我有DataGridRowDataGrid,如何从中获取各个列的值?请参阅下面的代码。

var itemsSource = MESearchDatagrid.ItemsSource as IEnumerable;
if (itemsSource != null)
{
    foreach (var item in itemsSource)
    {
        var row = MESearchDatagrid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
        if (row != null)
        {
        //How to get the Cell value?
        }

    }
}

【问题讨论】:

标签: c# wpf wpfdatagrid


【解决方案1】:

您可以将 row.item 转换为您的初始类类型。

        var itemsSource = MESearchDatagrid.ItemsSource;
        if (itemsSource != null)
        {
            foreach (var item in itemsSource)
            {
                var row = MESearchDatagrid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
                if (row != null)
                {
                    var data = row.Item as UserClass;
                    MessageBox.Show(data.Name);
                }

            }
        }

【讨论】:

    【解决方案2】:

    这里没有理由调用ContainerFromItem 方法。 ItemsSource 包含实际的数据对象。您只需要将它们转换为您的类型(在以下示例代码中称为 YourClass):

    var itemsSource = MESearchDatagrid.ItemsSource as IEnumerable;
    if (itemsSource != null)
    {
        foreach (var item in itemsSource.OfType<YourClass>())
        {
            var prop = item.Property1;
            //...
        }
    }
    

    Property1YourClass 的属性。

    【讨论】:

      猜你喜欢
      • 2011-04-09
      • 2013-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多