温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-07-10 15:35:33 来源:网络 阅读:317 作者:daniel8294 栏目:编程语言

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


    }

}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI