XAML Code
<window x:class= "Wpfapplication73.mainwindow"
Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns:d= "http://schemas.microsoft.com/expression/blend/2008"
Xmlns:mc= "http://schemas.openxmlformats.org/markup-compatibility/2006"
Xmlns:local= "Clr-namespace:wpfapplication73"
Mc:ignorable= "D"
Title= "MainWindow" height= "width=" 525 ">
<Grid>
<treeview itemssource= "{Binding}" >
<TreeView.Resources>
<textblock text= "{Binding year}"/>
</HierarchicalDataTemplate>
<stackpanel orientation= "Horizontal" >
<textblock text= "{Binding country}"/>
<textblock text= "{Binding date,stringformat=d}"/>
</StackPanel>
</HierarchicalDataTemplate>
<stackpanel orientation= "Horizontal" >
<textblock text= "{Binding Position}"/>
<textblock text= "{Binding Racer}"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
</Grid>
</Window>
Hide Code:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Windows;
Using System.Windows.Controls;
Using System.Windows.Data;
Using System.Windows.Documents;
Using System.Windows.Input;
Using System.Windows.Media;
Using System.Windows.Media.Imaging;
Using System.Windows.Navigation;
Using System.Windows.Shapes;
Namespace WpfApplication73
{
<summary>
Interaction Logic for MainWindow.xaml
</summary>
public partial class Mainwindow:window
{
Public formula1v2entities data = new Formula1v2entities ();
Public MainWindow ()
{
InitializeComponent ();
This. DataContext = years;
}
Public ienumerable<class1> years
{
Get
{
F1datacontext.data = Data;
return data. Races.select (r = new Class1
{
Year = R.date.year
}). Distinct (). (c = c.year). ToList ();
Return (from R in data. Races
Select New Class1
// {
Year= R.date.year
// }). ToList ();
}
}
}
}
The CLASS1 code that is used to produce the collection used by the TreeView control
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Namespace WpfApplication73
{
public static Class F1datacontext
{
public static formula1v2entities Data {get; set;}
}
public class Class1
{
public int year {get; set;}
Public ienumerable<f1race>races
{
Get
{
Return (from R in F1DataContext.Data.Races
where r.date.year = = Year
R.date
Select New F1race
{
Date = R.date,
Country = R.circuits.country
}). ToList ();
}
}
}
public class F1race
{
public string Country {get; set;}
Public DateTime Date {get; set;}
Public ienumerable<f1raceresult> Results
{
Get
{
Return (from RR in F1DataContext.Data.RaceResults
The Where RR. Races.date = = this. Date
Select New F1raceresult
{
Position = RR. Position,
Racer = RR. Racers.firstname + "" + RR. Racers.lastname
}). ToList ();
}
}
}
public class F1raceresult
{
public int Position {get; set;}
public string Racer {get; set;}
}
}
Application of the "C #" ADO. Net entities framework in the WPF TreeView