Search code examples
c#wpfdatagridcaliburn.micro

Wpf datagrid get underlying object cell value


I have a simple wpf datagrid in a caliburn micro project. I am initializing the rows with instances of a TestClass. If the user selects one of this row headers I want to get the instance of TestClass. But all values and all the example on the Internet are only able to show the Text in the cell.

So How do I get the object I used to create the cell from the datagrid?

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>

I want to get the object in the first selected cell and if it is of the type TestClass I want to have access to its Items property. Is this even possible?


Solution

  • To begin with, it is unclear why you are attempting use a DataTable with TestClass instances as row. For this to work, you need to figure out how to convert each property of TestClass to different columns in DataTable. A better approach would be to bind the DataGrid to a List<TestClass> directly.

    For example,

    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>();
    

    Now coming to your question of accessing the Selected Item, you could use the SelectedItem Property

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

    And in ViewModel

    public TestClass SelectedItem { get; set; }