WPF如何用TreeView制作好友列表、播放列表(转)
WPF如何用TreeView制作好友列表、播放列表
前言
TreeView这个控件对于我来说是用得比较多的,以前做的小聊天软件(好友列表)、音乐播放器(播放列表)、类库展示器(树形类结构)等都用的是TreeView,下面以一个好友列表为例,说明一下制作过程,这个过程可以搬迁到其他类似的场景中去(树形结构的场景)。
结果展示

制作过程
一:新建一个WPF工程,我命名为TreeViewDemo。(这步不解释)
二:准备图片,就是系统头像。在工程中新建一个Heads文件夹,并且添加系统头像图片,最后不要忘记需要把生成方式改为Resource、不复制,这样才能用标准的Pack URI的方式来读取到图片。(我喜欢用Pack Uri,因为比较通用,安全) 下载头像
三:好了,资源图片准备好了以后,来定义数据类,在这里我们需要用到两个类,一个代表好友,一个代表好友列表(对应的音乐播放器就是音乐和音乐列表)。
1.Friend(好友)

class Friend
{
public Friend(FriendList list)
{
this.List = list;
}
//头像的路径
public String HeadPath { set; get; }
//好友名字
public String Name { set; get; }
//签名
public String Autograph { set; get; } public FriendList List { set; get; } }

2.FriendList(好友列表)

class FriendList
{
public FriendList(String name)
{
this.ListName = name;
}
public String ListName { set; get; } private ObservableCollection<Friend> _friends = new ObservableCollection<Friend>();
public ObservableCollection<Friend> Friends
{
private set{}
get
{
return _friends;
}
} public void AddFriend(Friend newFriend)
{
_friends.Add(newFriend);
} public void RemoveFriend(Friend friend)
{
_friends.Remove(friend);
}
}

四:这一步是重点,就是在一个TreeView的节点中,如何去区分Friend和FriendList呢?我使用模板,我分别为Friend和FriendList定义属于它们各自的模板,一起定义在一个字典中,新建一个字典,我命名为GlobeDictionary。这里应该要用的是数据模板(DataTemplate),系统有一个数据模板叫HierarchicalDataTemplate,这个模板专门用于有头部和子数据的数据结构的,用在这个TreeView中刚好合适。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TreeViewDemo"> <HierarchicalDataTemplate DataType="{x:Type local:FriendList}" ItemsSource="{Binding Path=Friends,UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" x:Key="ListTemple" >
<Border x:Name="bord" Margin="-1,-1,0,-1">
<TextBlock Text="{Binding Path=ListName, Mode=TwoWay}" HorizontalAlignment="Left" Width="227" Padding="2" FontSize="15"></TextBlock>
</Border>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:Friend}" x:Key="FriendTemple">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40"></ColumnDefinition>
<ColumnDefinition Width="150"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Image Source="{Binding Path=HeadPath}" ></Image>
<StackPanel Orientation="Horizontal" Grid.Column="1" Margin="6,2,2,2">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Path=Name}" Height="25" HorizontalAlignment="Left" ></TextBlock>
<TextBlock Text="{Binding Path=Autograph}" Height="20" HorizontalAlignment="Left"></TextBlock>
</StackPanel>
</StackPanel>
</Grid>
</HierarchicalDataTemplate> </ResourceDictionary>

(注意:因为需要再这里引用自定义类,所以命名空间local的引入一定要正确,冲上面的定义可以看出,好友列表只显示名字,好友就用Grid来组织一个类QQ的布局,头像,签名等。)
五:主界面。
主界面比较简单,在这个Demo中,只使用TreeView控件。

<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:TreeViewDemo"
Title="MainWindow" Height="401" Width="305">
<Grid>
<TreeView x:Name="ListTV" ></TreeView>
</Grid>
</Window>

六:有了躯壳,我们填入灵魂(数据绑定和数据模板应用)。
模板有两个,直接填充TreeView的ItemTemplate属性是不行的,但是WPF中有一个思想很重要(Selector、Converter等),在TreeView中有一个属性ItemTemplateSelector。所以,新建一个Selector类,继承DataTemplateSelector:

class FriendOrListTemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
ResourceDictionary directory = new ResourceDictionary();
directory.Source = new Uri("pack://Application:,,,/GlobeDictionary.xaml", UriKind.RelativeOrAbsolute);//使用pack uri载入模板字典
if (item != null && item is FriendList)
{
return directory["ListTemple"] as DataTemplate;
}
return directory["FriendTemple"] as DataTemplate;
}
}

首先使用pack uri加载位于当前exe程序集中的字典资源(如果加载不了,请将字典生成方式改为Resource),然后根据传入的数据类型来返回对应的DataTemplate,这样就能有效的自动选择模板。
模板能自动选择了,另外数据怎么提供呢,我们使用在WPF中最常用的DataContext绑定来填充ItemsSource。下面看主界面的完整代码。

<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:TreeViewDemo"
Title="MainWindow" Height="401" Width="305">
<Grid>
<Grid.Resources>
<local:FriendOrListTemplateSelector x:Key="Myselector"></local:FriendOrListTemplateSelector>
</Grid.Resources>
<TreeView x:Name="ListTV"
ItemsSource="{Binding Path=DataContext, Mode=TwoWay,RelativeSource={RelativeSource Mode=Self}}"
ItemTemplateSelector="{StaticResource Myselector}" >
</TreeView>
</Grid>
</Window>

这里的命名空间local是FriendOrListTemplateSelector所在的命名空间中,请填写对。最后在主界面的代码中加入数据,测试一下。

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ObservableCollection<FriendList> Root = new ObservableCollection<FriendList>();
FriendList listOne = new FriendList("高中同学");
Friend friend1 = new Friend(listOne) { Autograph = "今天很开心,遇到了XX", HeadPath = "Heads/h1.png", Name = "天煞" };
Friend friend2 = new Friend(listOne) { Autograph = "非一般的感觉", HeadPath = "Heads/h2.png", Name = "Basx" };
Friend friend3 = new Friend(listOne) { Autograph = "WER", HeadPath = "Heads/h3.png", Name = "Paul" };
Friend friend4 = new Friend(listOne) { Autograph = "~终于完成作业了~~!", HeadPath = "Heads/h4.png", Name = "孤星月" };
listOne.AddFriend(friend1);
listOne.AddFriend(friend2);
listOne.AddFriend(friend3);
listOne.AddFriend(friend4);
FriendList listTwo = new FriendList("大一");
Friend friend5 = new Friend(listOne) { Autograph = "你的存在", HeadPath = "Heads/h5.png", Name = "菲菲" };
listTwo.AddFriend(friend5);
Root.Add(listOne);
Root.Add(listTwo);
this.ListTV.DataContext = Root;
}
}

大功告成~!
总结
过程比较简单,但是最重要的是学习其方法,有几点通用的知识需要注意的:
1.Xaml中需要使用自定义类的时候,需要在头部xmlns中添加一项,并且确保命名空间是对的。
2.Pack URI的使用,任何资源的引用都可以使用Pack URI,下面举两个例子:
*pack://Application:,,,/路径/路径/文件名(带后缀) <-这种方式是引用当前程序集的Resouce资源的。
*pack://Application:,,,/引用的程序集名称;component/路径/路径/文件名(带后缀)<-这种方式是引用其他程序集的Resouce资源的。
3.选择器(Selecter),转换器(Converter)等的思想要深入理解。
4.数据模板是为数据定制外观,例如这里就为Friend和FriendList定制了外观了。
转:http://www.cnblogs.com/Jarvin/p/3792171.html
WPF如何用TreeView制作好友列表、播放列表(转)的更多相关文章
- WPF如何用TreeView制作好友列表、播放列表
WPF如何用TreeView制作好友列表.播放列表 前言 TreeView这个控件对于我来说是用得比较多的,以前做的小聊天软件(好友列表).音乐播放器(播放列表).类库展示器(树形类结构)等都用的是T ...
- java在线聊天项目 swt可视化窗口Design 好友列表窗口
熟练使用各种布局方式 FlowLayout 流布局 left center right等 BorderLayout 边框布局 east west sorth north center Absolute ...
- Windows UIA自动化测试框架学习--获取qq好友列表
前段时间应公司要求开发一款针对现有WPF程序的自动化测试工具,在网上查资料找了一段时间,发现用来做自动化测试的框架还是比较多的,比如python的两个模块pywinauto和uiautomation, ...
- JTree实现好友列表
最近学习了一下JTree的使用方法: 先来看一下树的实例: 构建一个树, DefaultMutableTreeNode root = new DefaultMutableTreeNode(" ...
- OS开发UI篇—使用UItableview完成一个简单的QQ好友列表
本文转自:http://www.cnblogs.com/wendingding/p/3763330.html 一.项目结构和plist文件 二.实现代码 1.说明: 主控制器直接继承UITableVi ...
- XMPP框架下微信项目总结(5)花名册获取(好友列表)
---->概念 ---->添加花名册 ps:添加花名册,启动: 客户端发送请求到服务器获取好友列表信息,同时在项目中创建数据表,并保存好友列表到数据表中. ---->获取服务器保存好 ...
- 一步一步来做WebQQ机器人-(四)(获取好友列表和群列表)
× 本篇主要是: 获取好友列表,群列表 我会尽量详细一点,尽我所知的分享一些可能大家已经掌握的或者还不清楚的经验 利于大家阅读,文章样式不再复杂化,根据内容取固定色 目前总进度大概65% 全系列预计会 ...
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)
iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一) 一.项目结构和plist文件 二.实现代码 1.说明: 主控制器直接继承UITableViewController // ...
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(二)
一.实现效果 二.实现代码 1.数据模型部分 YYQQGroupModel.h文件 // // YYQQGroupModel.h // 02-QQ好友列表(基本数据的加载) / ...
随机推荐
- OnXXX函数与XXX事件的关系
OnPaint是Control类中的方法,Paint是事件,Paint是用于改变部分显示用比较合适,实际上Paint事件在OnPaint中被调用,如果你重写OnPaint但是不调用base.OnPai ...
- ArcGIS API for Silverlight/WPF 2.1学习笔记(一)——精简版
一.安装 1.Visual Studio: (1)Visual Studio 2010或Visual Web Developer Express 2010 (2)Silverlight 4 Tools ...
- Dajngo的CBV和FBV
CBV: class. base. view 路由: url(r'students/', views.StudentsView.as_view()) 视图: from django.views imp ...
- poj2417 Discrete Logging BSGS裸题
给a^x == b (mod c)求满足的最小正整数x, 用BSGS求,令m=ceil(sqrt(m)),x=im-j,那么a^(im)=ba^j%p;, 我们先枚举j求出所有的ba^j%p,1< ...
- Form嵌入到Panel里(C#)
直接把这个 Form嵌入到一个 Panel中即可. 示例如下: 要嵌入的 Form: public partial class FormEmbed : Form { public FormEmbed( ...
- throw new OAException执行了,却没有正常抛出异常!
try { if ("E".equals(returnStatus)) { throw new OAException(returnMessage, OAException.ERR ...
- [LeetCode] 35. Search Insert Position ☆(丢失的数字)
转载:https://leetcode.windliang.cc/leetCode-35-Search-Insert-Position.html 思路 Given a sorted array ...
- JavaScript运算符:递增递减运算符前置和后置的区别
从两段代码说起 var num1 = 2; var num2 = 20; var num3 = --num1 + num2; var num4 = num1 + num2; console.log(n ...
- 管道pipe与dup结合使用,应用实例
管道的一种常见用法:在父进程创建子进程后向子进程传递参数.例如,一个应用软件有一个主进程和很多个不同子进程. 主进程创建子进程后,在子进程调用exec函数执行一个新程序前,通过管道给即将执行的程序传递 ...
- bzoj1617
题解: dp f[i]表示运i头奶牛需要的时间 f[i]=f[i-j]+a[i]+2*m 然后ans=f[n]-m(最后一次不用跑回来) 代码: #include<bits/stdc++.h&g ...