【C#】ADO .Net Entities Framework在WPF TreeView中的应用

简介:


XAML代码

<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="350" Width="525">

    <Grid>

        <TreeView ItemsSource="{Binding}">

            <TreeView.Resources>

                <HierarchicalDataTemplate DataType="{x:Type local:Class1}" ItemsSource="{Binding Races}">

                 

                        <TextBlock Text="{Binding Year}"/>

                  

                </HierarchicalDataTemplate>

                

                <HierarchicalDataTemplate DataType="{x:Type local:F1Race}" ItemsSource="{Binding Results}">

                    <StackPanel Orientation="Horizontal">

                        <TextBlock Text="{Binding Country}"/>

                        <TextBlock Text="{Binding Date,StringFormat=d}"/>

                    </StackPanel>

                </HierarchicalDataTemplate>


                <HierarchicalDataTemplate DataType="{x:Type local:F1RaceResult}">

                    <StackPanel Orientation="Horizontal">

                        <TextBlock Text="{Binding Position}"/>

                        <TextBlock Text="{Binding Racer}"/>

                    </StackPanel>

                </HierarchicalDataTemplate>

            </TreeView.Resources>


        </TreeView>

    </Grid>

</Window>


隐藏代码:

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().OrderBy(c => c.Year).ToList();

                //return (from r in data.Races

                //        select new Class1

                //        {

                //           Year= r.Date.Year

                //        }).ToList();

            }

        }

    }

}


Class1代码,用来产生TreeView控件使用的集合

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

                           orderby 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

                        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; }


    }

}





      本文转自daniel8294 51CTO博客,原文链接:http://blog.51cto.com/acadia627/1737994,如需转载请自行联系原作者





相关文章
|
27天前
|
API C++ Windows
Visual C++运行库、.NET Framework和DirectX运行库的作用及常见问题解决方案,涵盖MSVCP140.dll丢失、0xc000007b错误等典型故障的修复方法
本文介绍Visual C++运行库、.NET Framework和DirectX运行库的作用及常见问题解决方案,涵盖MSVCP140.dll丢失、0xc000007b错误等典型故障的修复方法,提供官方下载链接与系统修复工具使用指南。
422 2
|
1月前
|
开发框架 安全 .NET
Microsoft .NET Framework 3.5、4.5.2、4.8.1,适用于 Windows 版本的 .NET,Microsoft C Runtime等下载
.NET Framework是Windows平台的开发框架,包含CLR和FCL,支持多种语言开发桌面、Web应用。常用版本有3.5、4.5.2、4.8.1,系统可同时安装多个版本,确保软件兼容运行。
491 0
Microsoft .NET Framework 3.5、4.5.2、4.8.1,适用于 Windows 版本的 .NET,Microsoft C Runtime等下载
|
2月前
|
C++
提示缺少.NET Framework 3.5 安装错误:0x80070002、0x800F0950\0x80004002
.NET Framework常见问题及解决方法汇总,
410 0
|
3月前
.NET Framework 3.5离线安装包合集下载
本文介绍了如何获取和安装.NET Framework运行库离线合集包。用户可通过提供的链接下载安装包,安装过程简单,按提示逐步操作即可完成。安装时可选择所需版本,工具会自动适配架构,无需手动判断,方便高效。
1423 0
|
C# 开发者 Windows
基于Material Design风格开源、易用、强大的WPF UI控件库
基于Material Design风格开源、易用、强大的WPF UI控件库
669 0
浅谈WPF之装饰器实现控件锚点
使用过visio的都知道,在绘制流程图时,当选择或鼠标移动到控件时,都会在控件的四周出现锚点,以便于修改大小,移动位置,或连接线等,那此功能是如何实现的呢?在WPF开发中,想要在控件四周实现锚点,可以通过装饰器来实现,今天通过一个简单的小例子,简述如何在WPF开发中,应用装饰器,仅供学习分享使用,如有不足之处,还请指正。
322 1
|
前端开发 C# 容器
浅谈WPF之控件拖拽与拖动
使用过office的visio软件画图的小伙伴都知道,画图软件分为两部分,左侧图形库,存放各种图标,右侧是一个画布,将左侧图形库的图标控件拖拽到右侧画布,就会生成一个新的控件,并且可以自由拖动。那如何在WPF程序中,实现类似的功能呢?今天就以一个简单的小例子,简述如何在WPF中实现控件的拖拽和拖动,仅供学习分享使用,如有不足之处,还请指正。
425 2
|
开发框架 缓存 前端开发
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(11) -- 下拉列表的数据绑定以及自定义系统字典列表控件
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(11) -- 下拉列表的数据绑定以及自定义系统字典列表控件
|
C# 开发者 Windows
一款基于Fluent设计风格、现代化的WPF UI控件库
一款基于Fluent设计风格、现代化的WPF UI控件库
375 1