日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

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

發布時間:2025/4/5 asp.net 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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中的应用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。