DataGrid : Binding with two different classes with lists ? WPF C#
Posted
by
MyRestlessDream
on Stack Overflow
See other posts from Stack Overflow
or by MyRestlessDream
Published on 2013-10-24T18:09:38Z
Indexed on
2013/10/24
21:55 UTC
Read the original article
Hit count: 141
It is my first question on StackOverflow so I hope I am doing nothing wrong ! Sorry if it is the case ! I need some help because I can not find the solution of my problem. Of course I have searched everywhere on the web but I can not find it (can not post the links that I am using because of my low reputation :( ). Moreover, I am new in C# and WPF (and self-learning). I used to work in C++/Qt so I do not know how everything works in WPF. And sorry for my English, I am French.
My problem
My basic classes are that an Employee can use a computer. The id of the computer and the date of use are stored into the class Connection
. I would like to display the list information in a DataGrid and in RowDetailsTemplate like here :
http://i.stack.imgur.com/Bvn1z.png
So it will do a binding to the Employee
class but also to the Connection
class with only the last value of the property (here the last value of the list "Computer ID" and the last value of the list "Connection Date" on this last computer). So it is a loop in the different lists. How can I do it ?
Is it too much to do ? :( I succeed to get the Employee informations but I do not know how to bind the list of computer. When I am trying, it shows me "(Collection)" so it does not go inside the list :(
Summary of Questions
- How to display/bind a value from a list AND from a different class in a
DataGrid
? - How to display all the values of a list into the
RowDetailsTemplate
?
Under Windows 7 and Visual Studio 2010 Pro version.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ EDIT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Solution
I have used the solution of Stefan Denchev. Here the modification of my class : http://i.stack.imgur.com/Ijx5i.png
And the code used:
<DataGrid ItemsSource="{Binding}" Name="table">
<DataGrid.Columns>
<DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"/>
<DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"/>
<DataGridTextColumn Header="Gender" Binding="{Binding Gender}"/>
<DataGridTextColumn Header="Last computer used" Binding="{Binding LastComputerID}"/>
<DataGridTextColumn Header="Last connection date" Binding="{Binding LastDate}"/>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DataGrid ItemsSource="{Binding ListOfConnection}">
<DataGrid.Columns>
<DataGridTextColumn Header="Computer ID" Binding="{Binding ComputerID}"/>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ListView ItemsSource="{Binding ListOfDate}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
With in code behind :
List<Employee> allEmployees = WorkflowMgr.Instance.AllEmployees;
table.DataContext = allEmployees;
And it works ! I have tryed to improve my fake example :) Hope it will help to another developer !
© Stack Overflow or respective owner