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;
}
}
}
}
WPF TreeView绑定xaml的写法的更多相关文章
- WPF TreeView绑定字典集合
<TreeView Name="Tree" HorizontalAlignment="Left" Height="269" Width ...
- 整理:WPF中Xaml中绑定枚举的写法
原文:整理:WPF中Xaml中绑定枚举的写法 目的:在Combobox.ListBox中直接绑定枚举对象的方式,比如:直接绑定字体类型.所有颜色等枚举类型非常方便 一.首先用ObjectDataPro ...
- 整理:WPF用于绑定命令和触发路由事件的自定义控件写法
原文:整理:WPF用于绑定命令和触发路由事件的自定义控件写法 目的:自定义一个控件,当点击按钮是触发到ViewModel(业务逻辑部分)和Xaml路由事件(页面逻辑部分) 自定义控件增加IComman ...
- 学习WPF——了解WPF中的XAML
XAML的简单说明 XAML是用于实例化.NET对象的标记语言,主要用于构建WPF的用户界面 XAML中的每一个元素都映射为.NET类的一个实例,例如<Button>映射为WPF的Butt ...
- WPF多路绑定
WPF多路绑定 多路绑定实现对数据的计算,XAML: 引用资源所在位置 xmlns:cmlib="clr-namespace:CommonLib;assembly=CommonLib&q ...
- WPF中 PropertyPath XAML 语法
原文:WPF中 PropertyPath XAML 语法 PropertyPath 对象支持复杂的内联XAML语法用来设置各种各样的属性,这些属性把PropertyPath类型作为它们的值.这篇文章讨 ...
- WPF元素绑定
原文:WPF元素绑定 数据绑定简介:数据绑定是一种关系,该关系告诉WPF从源对象提取一些信息,并用这些信息设置目标对象的属性.目标属性是依赖项属性.源对象可以是任何内容,从另一个WPF元素乃至ADO. ...
- WPF TreeView HierarchicalDataTemplate
原文 WPF TreeView HierarchicalDataTemplate HierarchicalDataTemplate 的DataType是本层的绑定,而ItemsSource是绑定下层的 ...
- 【广州.NET社区推荐】【译】Visual Studio 2019 中 WPF & UWP 的 XAML 开发工具新特性
原文 | Dmitry 翻译 | 郑子铭 自Visual Studio 2019推出以来,我们为使用WPF或UWP桌面应用程序的XAML开发人员发布了许多新功能.在本周的 Visual Studio ...
随机推荐
- XML输出到浏览器报错
在使用Firefox浏览器测试我编写的xml文件时,遇到如下错误:我的xml源代码如下: <?xml version="1.0" encoding="UTF-8&q ...
- 《从零開始学Swift》学习笔记(Day 1)——我的第一行Swift代码
Swift 2.0学习笔记--我的第一行Swift代码 原创文章,欢迎转载. 转载请注明:关东升的博客 当第一次看到以下代码时我石化了.这些代码是什么东东?单词拼出来的? import Foun ...
- php实现求扑克牌顺子(*****)(AC)(分类:把问题分小,利于排错)
php实现求扑克牌顺子(*****)(AC)(分类:把问题分小,利于排错) 一.总结 分类(那就可以把问题分小而逐步完成每个板块,这样是很简单的) 分类还有助于查错 二.php实现求扑克牌顺子 题目描 ...
- Android 比较两个时间段是否有交集或重复
先看一个例图: 在金山<电池管家>应用中就有一个类似上图这样的功能—— 开启多个定时任务. 当开启另一个定时任务的时候,如果即将开启的这个定时任务的时间段与已经开启了的定时任务的时间段有交 ...
- Android JNI -基础篇
JNI(Java Native Interface,JAVA本地接口) 可以使Java代码和其他语言写的代码(如C/C++代码)进行交互.为什么要进行交互? 首先,Java语言提供的类库无法满足要求, ...
- Android中密码输入内容可见性切换
今天在做项目的时候遇到了一个关于密码输入框可见性切换问题,上网搜了搜,这里面门道还不小,做一个记录吧,下次遇到就好解决了. 首先写了一个简单的测试工程: <LinearLayout xmlns: ...
- Google Guava官方教程
原文链接 译文链接 译者: 沈义扬,罗立树,何一昕,*武祖 * 校对:方腾飞 引言 Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库,例如:集合 [collections] . ...
- windows下 管理员身份启动java进程
http://blog.csdn.net/zhuyijian135757/article/details/38234757
- js实现去文本换行符小工具
js实现去文本换行符小工具 一.总结 一句话总结: 1.vertical属性使用的时候注意看清定义,也注意父元素的基准线问题.vertical-align:top; 2.获取textareaEleme ...
- Expression Blend 的点滴(4)--创建类似iPhone屏幕锁控件(下)
原文:Expression Blend 的点滴(4)--创建类似iPhone屏幕锁控件(下) 接着上篇... 接下去,将一步步演示如果创建当点击checkBox后,其中的按钮由左边滑动到右边,表示处于 ...