【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,如需转载请自行联系原作者





相关文章
|
4月前
|
算法 Java 调度
|
7月前
|
Kubernetes 关系型数据库 数据库
.netcore应用容器化部署
.netcore应用容器化部署
|
6月前
|
关系型数据库 MySQL 数据库
找不到请求的 .Net Framework Data Provider。可能没有安装
做的一个项目,框架为.net framework 数据库为mysql 出现如标题错误 检查是否安装mysql、是否安装mysql connector net 笔者是因为没有安装后者mysql connector net 下载地址: [mysql connector net](https://downloads.mysql.com/archives/c-net/ "mysql connector net") 笔者安装截图如下: ![请在此添加图片描述](https://developer-private-1258344699.cos.ap-guangzhou.myqcloud.com/c
56 0
|
7月前
|
安全 C# 开发工具
模拟.NET应用场景,综合应用反编译、第三方库调试、拦截、一库多版本兼容方案
模拟.NET实际应用场景,综合应用三个主要知识点:一是使用dnSpy反编译第三库及调试,二是使用Lib.Harmony库实现第三库拦截、伪造,三是实现同一个库支持多版本同时引用。
模拟.NET应用场景,综合应用反编译、第三方库调试、拦截、一库多版本兼容方案
|
6月前
|
C# Windows
[记录]c#.net framework 4.5调用运行时库
[记录]c#.net framework 4.5调用运行时库
|
2天前
|
开发框架 前端开发 JavaScript
采用C#.Net +JavaScript 开发的云LIS系统源码 二级医院应用案例有演示
技术架构:Asp.NET CORE 3.1 MVC + SQLserver + Redis等 开发语言:C# 6.0、JavaScript 前端框架:JQuery、EasyUI、Bootstrap 后端框架:MVC、SQLSugar等 数 据 库:SQLserver 2012
|
1月前
|
Windows
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
|
1月前
|
XML 数据可视化 C#
C# .NET面试系列五:WPF
<h2>WPF #### 1. WPF 由哪两部分组成? Windows Presentation Foundation (WPF) 由两个主要部分组成: 1、XAML (eXtensible Application Markup Language) ```c# 这是一种基于 XML 的标记语言,用于定义用户界面的结构和外观。XAML允许开发人员使用声明性语法来描述应用程序的用户界面元素,而不是使用传统的编程方式。XAML 被广泛用于定义 WPF 窗体、控件、布局和动画。 ``` 2、Code-behind 文件 ```c# 这是包含与用户界面相关逻辑的代码文件。通常,开发人员可
73 4
|
6月前
|
Windows
​史上最详细的Windows10系统离线安装.NET Framework 3.5的方法(附离线安装包下载)
​史上最详细的Windows10系统离线安装.NET Framework 3.5的方法(附离线安装包下载)
529 0
|
6月前
|
Go
Golang 语言怎么使用 net/http 标准库开发 http 应用?
Golang 语言怎么使用 net/http 标准库开发 http 应用?
26 0