首页 > 代码库 > D22_03_TreeView控件(ICollection借口)
D22_03_TreeView控件(ICollection借口)
<Window x:Class="demo.BoundTreeView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="BoundTreeView" Height="300" Width="300" xmlns:data="clr-namespace:StoreDatabase;assembly=StoreDatabase" > <Grid> <TreeView Name="treeCategories" Margin="5"> <TreeView.ItemTemplate> <!--HierarchicalDataTemplate显示第一层控制代码ItemsSource,ItemsSource绑定到一对多的多段--> <HierarchicalDataTemplate ItemsSource="{Binding Path=Products}"> <StackPanel Orientation="Horizontal"> <CheckBox></CheckBox> <Image Source="/demo;component/Images/Chrysanthemum.jpg" Width="10" Height="10"></Image> <TextBlock Text="{Binding Path=CategoryName}"></TextBlock> </StackPanel> <!--第二层控制代码--> <HierarchicalDataTemplate.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <CheckBox></CheckBox> <Image Source="/demo;component/Images/Chrysanthemum.jpg" Width="10" Height="10"></Image> <TextBlock Text="{Binding Path=ModelName}"></TextBlock> <Image Source="/demo;component/Images/Chrysanthemum.jpg" Width="10" Height="10"></Image> </StackPanel> </DataTemplate> </HierarchicalDataTemplate.ItemTemplate> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView> </Grid></Window>
BoundTreeView
using System;using System.Collections.Generic;using System.Linq;using System.Text;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.Shapes;namespace demo{ /// <summary> /// BoundTreeView.xaml 的交互逻辑 /// </summary> public partial class BoundTreeView : Window { public BoundTreeView() { InitializeComponent(); treeCategories.ItemsSource = App.StoreDb.GetCategoriesAndProducts(); } }}
StoreDatabase.StoreDB
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Data.SqlClient;using System.Collections.ObjectModel;namespace StoreDatabase{ public class StoreDB { private string connectionString = StoreDatabase.Properties.Settings.Default.Store; public ICollection<Product> GetProducts() { SqlConnection con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand("GetProducts", con); cmd.CommandType = CommandType.StoredProcedure; ObservableCollection<Product> products = new ObservableCollection<Product>(); try { con.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { // Create a Product object that wraps the // current record. Product product = new Product((string)reader["ModelNumber"], (string)reader["ModelName"], (decimal)reader["UnitCost"], (string)reader["Description"], (int)reader["ProductID"], (string)reader["CategoryName"], (string)reader["ProductImage"]); // Add to collection products.Add(product); } } finally { con.Close(); } return products; } public ICollection<Category> GetCategoriesAndProducts() { SqlConnection con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand("GetProducts", con); cmd.CommandType = CommandType.StoredProcedure; SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); adapter.Fill(ds, "Products"); cmd.CommandText = "GetCategories"; adapter.Fill(ds, "Categories"); // Set up a relation between these tables (optional). DataRelation relCategoryProduct = new DataRelation("CategoryProduct", ds.Tables["Categories"].Columns["CategoryID"], ds.Tables["Products"].Columns["CategoryID"]); ds.Relations.Add(relCategoryProduct); ObservableCollection<Category> categories = new ObservableCollection<Category>(); foreach (DataRow categoryRow in ds.Tables["Categories"].Rows) { ObservableCollection<Product> products = new ObservableCollection<Product>(); foreach (DataRow productRow in categoryRow.GetChildRows(relCategoryProduct)) { products.Add(new Product(productRow["ModelNumber"].ToString(), productRow["ModelName"].ToString(), (decimal)productRow["UnitCost"], productRow["Description"].ToString())); } categories.Add(new Category(categoryRow["CategoryName"].ToString(), products)); } return categories; } }}
Category
using System;using System.Collections.Generic;using System.Text;using System.Collections.ObjectModel;using System.ComponentModel;namespace StoreDatabase{ public class Category : INotifyPropertyChanged { private string categoryName; public string CategoryName { get { return categoryName; } set { categoryName = value; OnPropertyChanged(new PropertyChangedEventArgs("CategoryName")); } } private ObservableCollection<Product> products; public ObservableCollection<Product> Products { get { return products; } set { products = value; OnPropertyChanged(new PropertyChangedEventArgs("Products")); } } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(PropertyChangedEventArgs e) { if (PropertyChanged != null) PropertyChanged(this, e); } public Category(string categoryName, ObservableCollection<Product> products) { CategoryName = categoryName; Products = products; } }}
Product
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ComponentModel;namespace StoreDatabase{ public class Product : INotifyPropertyChanged { private string modelNumber; public string ModelNumber { get { return modelNumber; } set { modelNumber = value; OnPropertyChanged(new PropertyChangedEventArgs("ModelNumber")); } } private string modelName; public string ModelName { get { return modelName; } set { modelName = value; OnPropertyChanged(new PropertyChangedEventArgs("ModelName")); } } private decimal unitCost; public decimal UnitCost { get { return unitCost; } set { unitCost = value; OnPropertyChanged(new PropertyChangedEventArgs("UnitCost")); } } private string description; public string Description { get { return description; } set { description = value; OnPropertyChanged(new PropertyChangedEventArgs("Description")); } } private string categoryName; public string CategoryName { get { return categoryName; } set { categoryName = value; } } // For DataGridComboBoxColumn example. private int categoryID; public int CategoryID { get { return categoryID; } set { categoryID = value; } } private string productImagePath; public string ProductImagePath { get { return productImagePath; } set { productImagePath = value; } } public Product(string modelNumber, string modelName, decimal unitCost, string description) { ModelNumber = modelNumber; ModelName = modelName; UnitCost = unitCost; Description = description; } public Product(string modelNumber, string modelName, decimal unitCost, string description, string productImagePath) : this(modelNumber, modelName, unitCost, description) { ProductImagePath = productImagePath; } public Product(string modelNumber, string modelName, decimal unitCost, string description, int categoryID, string categoryName, string productImagePath) : this(modelNumber, modelName, unitCost, description) { CategoryName = categoryName; ProductImagePath = productImagePath; CategoryID = categoryID; } public override string ToString() { return ModelName + " (" + ModelNumber + ")"; } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(PropertyChangedEventArgs e) { if (PropertyChanged != null) PropertyChanged(this, e); } // This for testing date editing. The value isn‘t actually stored in the database. private DateTime dateAdded = DateTime.Today; public DateTime DateAdded { get { return dateAdded; } set { dateAdded = value; } } }}
存储过程
create PROCEDURE [dbo].[GetCategories] ASSELECT * FROM Categoriescreate PROCEDURE [dbo].[GetProducts] ASSELECT * FROM Products INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID
D22_03_TreeView控件(ICollection借口)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。