获取WPF中DataGridCellInfo的项目

huangapple go评论52阅读模式
英文:

Get Item from DataGridCellInfo in WPF

问题

我应该如何从字段“Name”中获取值?

英文:

I have a DataGrid in WPF. The DataGridCellInfo contains Column, IsValid and Item.

private void ProjectsDatagrid_SelectionChanged(object sender, MouseButtonEventArgs e) {
            var selected = ProjectsDatagrid.SelectedCells[0];
        }

I tried:

var foo = selected.Item.Name

var foo2 = selected.Item[Name]

How am I supposed to get the value from the field Name?

答案1

得分: 0

Item转换为您的类型,例如:

var dataObject = selected.Item as YourClass;
if (dataObject != null)
{
    string name = dataObject.Name;
    //...
}

这里的YourClass是一个具有Name属性的自定义类。

英文:

Cast Item to your type, e.g.:

var dataObject = selected.Item as YourClass;
if (dataObject != null)
{
    string name = dataObject.Name;
    //...
}

...where YourClass is a custom class with a Name property.

huangapple
  • 本文由 发表于 2023年5月17日 16:00:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定