繁体   English   中英

Wpf datagrid 获取底层对象单元格值

[英]Wpf datagrid get underlying object cell value

我在 caliburn 微型项目中有一个简单的 wpf 数据网格。 我正在用 TestClass 的实例初始化行。 如果用户选择此行标题之一,我想获取 TestClass 的实例。 但是网上所有的值和所有的例子都只能显示单元格中的Text。

那么如何从数据网格中获取用于创建单元格的对象呢?

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>

我想在第一个选定的单元格中获取对象,如果它是 TestClass 类型,我想访问它的 Items 属性。 这甚至可能吗?

首先,不清楚您为什么尝试使用带有 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>();

现在谈到访问 Selected Item 的问题,您可以使用SelectedItem属性

<DataGrid
            ItemsSource="{Binding ProfileColumnRows}" SelectedItem="{Binding SelectedItem}"
            />

在 ViewModel 中

public TestClass SelectedItem { get; set; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM