Wpf datagrid 获取底层 object 单元格值
Wpf datagrid get underlying object cell value
我在 Caliburn 微型项目中有一个简单的 wpf 数据网格。我正在用 TestClass 的实例初始化行。如果用户选择这一行之一 headers 我想获取 TestClass 的实例。但是网上所有的值和所有的例子都只能显示单元格中的文本。
那么如何获取我用来从数据网格创建单元格的 object?
ShellViewModel.cs:
using System.Collections.Generic;
using System.Data;
using System.Windows.Controls;
using Caliburn.Micro;
namespace TestSelectionChanged.ViewModels
{
class ShellViewModel : Screen
{
private DataTable _profileColumnRows;
public DataTable ProfileColumnRows
{
get => _profileColumnRows;
set
{
if (Equals(value, _profileColumnRows)) return;
_profileColumnRows = value;
NotifyOfPropertyChange();
}
}
public ShellViewModel()
{
ProfileColumnRows = new DataTable("test");
ProfileColumnRows.Columns.Add("test1");
ProfileColumnRows.Columns.Add("test2");
// Here are the TestClass objects I want to get later
ProfileColumnRows.Rows.Add(new TestClass("testc","a","b"));
ProfileColumnRows.Rows.Add(new TestClass("testd", "c", "d"));
}
public void OnSelectionCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
var dataGrid = ((DataGrid) sender);
var column = dataGrid.SelectedCells[0].Column;
var columnIndex = dataGrid.SelectedCells[0].Column.DisplayIndex;
var rowIndex = dataGrid.Items.IndexOf(dataGrid.SelectedCells[0].Item);
}
}
class TestClass
{
public string name { get; set; }
public List<string> Items { get; set; }
public TestClass(string name, params string[] items)
{
this.name = name;
Items = new List<string>(items);
}
public override string ToString()
{
return name;
}
}
}
ShellView.xaml:
<UserControl x:Class="TestSelectionChanged.Views.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:cal="http://www.caliburnproject.org"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<StackPanel>
<DataGrid
ItemsSource="{Binding ProfileColumnRows}"
cal:Message.Attach="[Event SelectedCellsChanged] = [Action OnSelectionCellsChanged($source,$eventArgs)]"/>
</StackPanel>
</UserControl>
我想在第一个选定的单元格中获取 object,如果它是 TestClass 类型,我想访问它的项目 属性。这甚至可能吗?
首先,不清楚您为什么要尝试使用带有 TestClass 实例作为行的 DataTable。为此,您需要弄清楚如何将 TestClass 的每个 属性 转换为 DataTable 中的不同列。更好的方法是将 DataGrid 直接绑定到 List<TestClass>
。
例如,
public ShellViewModel()
{
ProfileColumnRows.Add(new TestClass("testc", "a", "b"));
ProfileColumnRows.Add(new TestClass("testd", "c", "d"));
}
public List<TestClass> ProfileColumnRows {get;set;} = new List<TestClass>();
现在谈到您访问所选项目的问题,您可以使用 SelectedItem
属性
<DataGrid
ItemsSource="{Binding ProfileColumnRows}" SelectedItem="{Binding SelectedItem}"
/>
在 ViewModel 中
public TestClass SelectedItem { get; set; }
我在 Caliburn 微型项目中有一个简单的 wpf 数据网格。我正在用 TestClass 的实例初始化行。如果用户选择这一行之一 headers 我想获取 TestClass 的实例。但是网上所有的值和所有的例子都只能显示单元格中的文本。
那么如何获取我用来从数据网格创建单元格的 object?
ShellViewModel.cs:
using System.Collections.Generic;
using System.Data;
using System.Windows.Controls;
using Caliburn.Micro;
namespace TestSelectionChanged.ViewModels
{
class ShellViewModel : Screen
{
private DataTable _profileColumnRows;
public DataTable ProfileColumnRows
{
get => _profileColumnRows;
set
{
if (Equals(value, _profileColumnRows)) return;
_profileColumnRows = value;
NotifyOfPropertyChange();
}
}
public ShellViewModel()
{
ProfileColumnRows = new DataTable("test");
ProfileColumnRows.Columns.Add("test1");
ProfileColumnRows.Columns.Add("test2");
// Here are the TestClass objects I want to get later
ProfileColumnRows.Rows.Add(new TestClass("testc","a","b"));
ProfileColumnRows.Rows.Add(new TestClass("testd", "c", "d"));
}
public void OnSelectionCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
var dataGrid = ((DataGrid) sender);
var column = dataGrid.SelectedCells[0].Column;
var columnIndex = dataGrid.SelectedCells[0].Column.DisplayIndex;
var rowIndex = dataGrid.Items.IndexOf(dataGrid.SelectedCells[0].Item);
}
}
class TestClass
{
public string name { get; set; }
public List<string> Items { get; set; }
public TestClass(string name, params string[] items)
{
this.name = name;
Items = new List<string>(items);
}
public override string ToString()
{
return name;
}
}
}
ShellView.xaml:
<UserControl x:Class="TestSelectionChanged.Views.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:cal="http://www.caliburnproject.org"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<StackPanel>
<DataGrid
ItemsSource="{Binding ProfileColumnRows}"
cal:Message.Attach="[Event SelectedCellsChanged] = [Action OnSelectionCellsChanged($source,$eventArgs)]"/>
</StackPanel>
</UserControl>
我想在第一个选定的单元格中获取 object,如果它是 TestClass 类型,我想访问它的项目 属性。这甚至可能吗?
首先,不清楚您为什么要尝试使用带有 TestClass 实例作为行的 DataTable。为此,您需要弄清楚如何将 TestClass 的每个 属性 转换为 DataTable 中的不同列。更好的方法是将 DataGrid 直接绑定到 List<TestClass>
。
例如,
public ShellViewModel()
{
ProfileColumnRows.Add(new TestClass("testc", "a", "b"));
ProfileColumnRows.Add(new TestClass("testd", "c", "d"));
}
public List<TestClass> ProfileColumnRows {get;set;} = new List<TestClass>();
现在谈到您访问所选项目的问题,您可以使用 SelectedItem
属性
<DataGrid
ItemsSource="{Binding ProfileColumnRows}" SelectedItem="{Binding SelectedItem}"
/>
在 ViewModel 中
public TestClass SelectedItem { get; set; }