【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; }
}
}
转载于:https://blog.51cto.com/acadia627/1737994
【C#】ADO .Net Entities Framework在WPF TreeView中的应用相关推荐
- 在WPF TreeView中使用复选框
目录 介绍 背景 细节决定成败 功能要求 将Smarts放入ViewModel 树视图配置 将TreeViewItem变成ToggleButton Aero主题中的复选框错误 介绍 本文回顾了一个WP ...
- 【C#】ADO .Net Entities Framework使用查询语句时遇到的错误
XAML代码: <Window x:Class="WpfApplication72.MainWindow" xmlns="http://schemas.micros ...
- ADO Entities Framework不对多表查询进行优化?
项目中用ADO Entities Framework来作为数据库表和实体的映射框架,对单表到3个关联表的增.删.改和查询操作,不用再用代码生成器去生成实体类和数据库操作类,简单逻辑功能实现起来,几乎 ...
- ADO.NET Entity Framework 学习(1) [ZT]
前一段时间园子里有很多文章介绍分析了Linq To SQL,它可以说是一个简单实现的ORM,是微软针对数据和对象的阻抗不平衡的问题.C# 3.0推出一些新的特性,比如Extension Method, ...
- ADO.NET Entity Framework 简介
一直对EF都是一知半解的,没有系统的了解过EF有什么样的功能,有什么具体的好处.在接下来的文章中会详细介绍EF.以下是参考MSDN上的文章,翻译并加以自己的理解得出的. ADO.NET Entity ...
- ADO.NET Entity Framework Beta2(五)/快速入门(实体框架)
This quickstart illustrates a series of tasks that support the topics in Getting Started with the En ...
- ADO.NET Entity Framework 基本概述
时间过得很快转眼间VS已经2010版了,在4月12日将会正式发布VS 2010 ADOEF 做为.Net 4.0 中被微软推荐的ORM框架,相比.Net 3.5 sp1 已经得到了进一步的增强,使用它 ...
- ADO.NET Entity Framework 深入分析, Part 5
ADO.NET Entity Framework 深入分析, Part 5 前面的Part 1-4的文章,介绍了Entity Data Model.Entity SQL.ObjectQuery.Ent ...
- WPF:使用Json.NET在TreeView中树形显示JSON数据
原文 WPF:使用Json.NET在TreeView中树形显示JSON数据 据 读者可以参考这个开源的可以树形显示XML和JSON的工具: Mgen Object 603:XML/JSON树形显示小工 ...
最新文章
- FM-2018-大熊猫的肠道菌群可能并没有特化出发酵纤维素的能力
- 组件赋值——@Value ---@PropertySource(value={“classpath:/person.properties“})
- SERV-U FTP SERVER和SERV-U MFT SERVER的区别
- win10专业版占多少空间_win10技巧:使用win10轻松自动恢复系统!
- 五、朴素贝叶斯分类算法
- iview的走马灯嵌套在模态框中,宽度为0的解决方案
- 提升对前端的认知,不得不了解Web API的DOM和BOM
- 前端学习(1382):多人管理项目2案例初始化
- gbdt 回归 特征重要性 排序_gbdt、xgb、lgb、cat面经整理——from牛客
- 新松机器人电气三天考核_比技能更比匠心,3天内高手的这些作品令人惊叹……...
- 性能提升160%,为全球提供顶级算力:阿里云发布第三代神龙云服务器
- Shopee开店首站有哪些站点?虾皮入驻规则
- dell进入u盘启动模式_戴尔Dell笔记本电脑的BIOS怎么设置从U盘启动
- 计算机专业买啥样笔记本,高考结束,想学计算机专业,买台啥样的笔记本比较好,暑假自学c和cpp...
- vue 管理系统顶部tags浏览历史实现
- (Linux目录操作命令)零基础小白学习_入门到精通03 程序员阿沐
- 【问题小记】“安装打印机驱动后,依然不可打印”故障排除
- jvm虚拟机-什么是jvm
- 2021年科学突破奖:生命科学、基础物理学和数学奖得主揭晓
- win7实现局域网共享文件