WPF TreeView绑定xaml的写法(转)

WPF TreeView绑定xaml的写法

方法一

<Window x:Class="TreeViewDemo.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" >
    <Grid>
        <TreeView Name="TreeCategories" Margin="5">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Path=Products}">
                    <TextBlock Text="{Binding Path=CategoryName}"></TextBlock>
                    <HierarchicalDataTemplate.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=ModelName}"></TextBlock>
                        </DataTemplate>
                    </HierarchicalDataTemplate.ItemTemplate>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Grid>

</Window>

方法二

<Window x:Class="TreeViewDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DBAccess;assembly=DBAccess"
        Title="MainWindow" Height="350" Width="525" >
    <Window.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:Category}" ItemsSource="{Binding Path=Products}">
            <TextBlock Text="{Binding Path=CategoryName}"></TextBlock>
        </HierarchicalDataTemplate>
        
        <HierarchicalDataTemplate DataType="{x:Type local:Product}">
            <TextBlock Text="{Binding Path=ModelName}"></TextBlock>
        </HierarchicalDataTemplate>
    </Window.Resources>
    <Grid>
        <TreeView Name="TreeCategories" Margin="5">
            
        </TreeView>
    </Grid>
</Window>

using DBAccess;
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 TreeViewDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            TreeCategories.ItemsSource = StoreDB.GetCategoriedAndProducts();
        }
    }

}

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace DBAccess
{
    public class StoreDB
    {
        public static string connStr = Properties.Settings.Default.Store;


        public static ObservableCollection<Product> GetProducts()
        {
            ObservableCollection<Product> products = new ObservableCollection<Product>();
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                SqlCommand cmd = new SqlCommand("GetProducts", conn);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    products.Add(new Product((int)reader["ProductID"],(int)reader["CategoryID"],reader["ModelNumber"].ToString(),
                        reader["ModelName"].ToString(),reader["ProductImage"].ToString(),(decimal)reader["UnitCost"],reader["Description"].ToString()));
                }


                return products;
            }
        }




        public static Product GetProductByID(int ProductID)
        {
            Product pro =null;
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                SqlCommand cmd = new SqlCommand("GetProductByID", conn);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ProductID",ProductID));
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    pro = new Product((int)reader["ProductID"], (int)reader["CategoryID"], reader["ModelNumber"].ToString(),
                        reader["ModelName"].ToString(), reader["ProductImage"].ToString(), (decimal)reader["UnitCost"], reader["Description"].ToString());
                }


                return pro;
            }
        }


        public static bool UpdateProduct(Product pro)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                SqlCommand cmd = new SqlCommand("UpdateProductByID", conn);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ProductID", pro.ProductID));
                cmd.Parameters.Add(new SqlParameter("@ModelName", pro.ModelName));
                cmd.Parameters.Add(new SqlParameter("@ModelNumber", pro.ModelNumber));
                cmd.Parameters.Add(new SqlParameter("@UnitCost", pro.UnitCost));
                cmd.Parameters.Add(new SqlParameter("@Description", pro.Description));


                conn.Open();
                return cmd.ExecuteNonQuery() > 0;
                
            }
        }




        public static DataTable GetProductsDataTable()
        {
            using(SqlConnection conn = new SqlConnection(connStr))
            {
                SqlCommand cmd = new SqlCommand("GetProducts", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapter.Fill(ds, "Products");
                return ds.Tables["Products"];
            }
        }


        public static DataSet GetCategoriedAndProductsDataSet()
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                SqlCommand cmd = new SqlCommand("GetProducts", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapter.Fill(ds, "Products");


                cmd.CommandText = "GetCategories";
                adapter.Fill(ds, "Categories");


                DataRelation dr = new DataRelation("CategoryProduct", ds.Tables["Categories"].Columns["CategoryID"], ds.Tables["Produdcts"].Columns["CategoryID"]);
                ds.Relations.Add(dr);
                return ds;
            }
        }


        public static ICollection<Category> GetCategoriedAndProducts()
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                SqlCommand cmd = new SqlCommand("GetProducts", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapter.Fill(ds, "Products");


                cmd.CommandText = "GetCategories";
                adapter.Fill(ds, "Categories");


                DataRelation dr = new DataRelation("CategoryProduct", ds.Tables["Categories"].Columns["CategoryID"], ds.Tables["Products"].Columns["CategoryID"]);
                ds.Relations.Add(dr);
                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(dr))
                    {
                        products.Add(new Product((int)productRow["ProductID"],(int)productRow["CategoryID"],productRow["ModelNumber"].ToString(),productRow["ModelName"].ToString(),productRow["ProductImage"].ToString(),(decimal)productRow["UnitCost"],productRow["Description"].ToString()));
                        
                    }
                    categories.Add(new Category(categoryRow["CategoryName"].ToString(),products));
                }


                return categories;
                
            }
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/LiZhongZhongY/p/10896939.html