Mafii
Mafii

Reputation: 7455

Display row details as datagrid

I have a datagrid, which items are list of (just an example for easier understanding):

public class Person
{
  public string FirstName { get; set; }
  public string LastName { get; set; }

  public List<Company> Companies { get; set; }
}

public class Company 
{
  public string Name { get; set; }
  public string City { get; set; }
}

And now I want to display the companies in my RowDetailsTemplate.

I have tried to do this

<DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <DataGrid ItemsSource="{Binding Companies}">
                [im trying to access the data of the row here]
            </DataGrid>
        </DataTemplate>
</DataGrid.RowDetailsTemplate>

And my ItemsSource of the datagrid is a List (its readonly, its just to display, not edit data.)

My problem is, that I can't seem to access the Companies, but rather VS/R# only says that the List<Person> (with the name Persons inside of my viewModel) is available.

But I want to access the data per row, not every the data of all the rows. How do I do that?

Upvotes: 1

Views: 4736

Answers (1)

Gopichandar
Gopichandar

Reputation: 2841

Let me explain you with an example. Copy paste the following code and put the breakpoint on get and set of SelectedEmployee.

Now run the code and click the different row. You will get the resepctive row item with list of details in it.

MainWindow.xaml

    <DataGrid ItemsSource="{Binding Employees}" SelectedItem="{Binding SelectedEmployee}" >
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <DataGrid ItemsSource="{Binding Details}"/>
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>
    </DataGrid>

MainWindow.xaml.cs

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var detail1 = new EmployeeDetails() { ManagerID = 11, ManagerName = "11 Name", ManagerMobile = "123456" };
        var detail2 = new EmployeeDetails() { ManagerID = 12, ManagerName = "12 Name", ManagerMobile = "123456" };
        var detail3 = new EmployeeDetails() { ManagerID = 13, ManagerName = "13 Name", ManagerMobile = "123456" };

        var detail4 = new EmployeeDetails() { ManagerID = 11, ManagerName = "11 Name", ManagerMobile = "123456" };
        var detail5 = new EmployeeDetails() { ManagerID = 12, ManagerName = "12 Name", ManagerMobile = "123456" };
        var detail6 = new EmployeeDetails() { ManagerID = 13, ManagerName = "13 Name", ManagerMobile = "123456" };

        var detail7 = new EmployeeDetails() { ManagerID = 11, ManagerName = "11 Name", ManagerMobile = "123456" };
        var detail8 = new EmployeeDetails() { ManagerID = 12, ManagerName = "12 Name", ManagerMobile = "123456" };
        var detail9 = new EmployeeDetails() { ManagerID = 13, ManagerName = "13 Name", ManagerMobile = "123456" };

        var details1 = new List<EmployeeDetails>();
        details1.Add(detail1);
        details1.Add(detail2);
        details1.Add(detail3);

        var details2 = new List<EmployeeDetails>() { detail4, detail5, detail6 };
        var details3 = new List<EmployeeDetails>() { detail7, detail8, detail9 };

        Employees = new List<Employee>();
        Employees.Add(new Employee() { ID = 1, Name = "Name1", Details = details1 });
        Employees.Add(new Employee() { ID = 2, Name = "Name2", Details = details2 });
        Employees.Add(new Employee() { ID = 3, Name = "Name3", Details = details3 });            

        SelectedEmployee = Employees[1];

        this.DataContext = this;
    }

    public List<Employee> Employees { get; set; }

    private Employee _selected;

    public Employee SelectedEmployee
    {
        get { return _selected; }
        set { _selected = value; }
    }


}

public class Employee
{
    public int ID { get; set; }
    public string Name { get; set; }
    public List<EmployeeDetails> Details { get; set; }
}

public class EmployeeDetails
{
    public int ManagerID { get; set; }
    public string ManagerName { get; set; }
    public string ManagerMobile { get; set; }

}

Upvotes: 4

Related Questions