三、数据绑定与模板样式
说明:通过学习本文内容,您将了解到怎样动态地进行关于TreeView组件的数据绑定以及TreeViewItem组件的模板样式更改。本文给出基本的使用方法。

 

:在Silverlight项目文件夹下建立Icon文件夹,向里面添加3个16*16的png格式的图像。关于动态数据绑定这里只给出了基本的使用方法,在实际开发中可以使用WebService、XML等进行数据绑定。

效果图


Feature.cs(
业务模型)代码

using System.Collections.ObjectModel;

using System.ComponentModel;

using System.Windows.Markup;

namespace SilverlightClient

{

[ContentProperty("Subcomponents")] //声明可在XAML文件中显示的内容属性

public class Feature : INotifyPropertyChanged //继承接口INotifyPropertyChanged用于双向数据绑定

{

//Feature对象的属性

public string FeatureName { get; set; }

public string Description { get; set; }

public string Icon { get; set; }//用以添加TreeViewItem项的图标

//声明全局变量

public Collection<Feature> Subcomponents { get; private set; }

private bool? _shouldInstall;

//是否有子组件

public bool HasSubcomponents

{

get

{

return Subcomponents.Count > 0;

}

}

//是否允许Feature进行安置

public bool? ShouldInstall

{

get

{

return _shouldInstall;

}

set

{

if (value != _shouldInstall)

{

_shouldInstall = value;

OnPropertyChanged("ShouldInstall");

}

}

}

//构造函数

public Feature()

{

Subcomponents = new Collection<Feature>();

ShouldInstall = true;

}

//事件委托

public event PropertyChangedEventHandler PropertyChanged;

//实现接口INotifyPropertyChanged定义函数

private void OnPropertyChanged(string propertyName)

{

PropertyChangedEventHandler handler = PropertyChanged;

if (null != handler)

{

handler.Invoke(this, new PropertyChangedEventArgs(propertyName));

}

}

}

}

MainPage.xaml文件代码

<UserControl

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:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"

xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"

xmlns:common="clr-namespace:System.Windows;assembly=System.Windows.Controls"

xmlns:samplesCommon="clr-namespace:SilverlightClient"

mc:Ignorable="d" x:Class="SilverlightClient.MainPage"

Width="640" Height="480">

<Grid x:Name="LayoutRoot" Background="White" Width="640" Height="480">

<StackPanel>

<StackPanel.Resources>

<!-- TreeViewItem风格设定 -->

<Style x:Key="PurpleItemStyle" TargetType="controls:TreeViewItem">

<Setter Property="Foreground" Value="Purple" />

<Setter Property="FontWeight" Value="Bold" />

</Style>

<!-- 代表一个Feature项的模板 -->

<common:HierarchicalDataTemplate x:Key="NodeTemplate" ItemsSource="{Binding Subcomponents}" ItemContainerStyle="{StaticResource PurpleItemStyle}">

<StackPanel Orientation="Horizontal" ToolTipService.ToolTip="{Binding Description}">

<Image Source="{Binding Icon}" /><!-- 图标绑定 -->

<ContentPresenter Content=" " />

<ContentPresenter Content="{Binding FeatureName}" />

</StackPanel>

</common:HierarchicalDataTemplate>

</StackPanel.Resources>

<Grid>

<Grid.ColumnDefinitions>

<ColumnDefinition Width="*" />

<ColumnDefinition Width="2*" />

</Grid.ColumnDefinitions>

<controls:TreeView x:Name="tvFeature"

ItemTemplate="{StaticResource NodeTemplate}"

Grid.Column="0"

FontSize="14">

<!-- 用来一次展开TreeView所有结点 -->

<controls:TreeView.ItemContainerStyle>

<Style TargetType="controls:TreeViewItem">

<Setter Property="IsExpanded" Value="True" />

</Style>

</controls:TreeView.ItemContainerStyle>

</controls:TreeView>

</Grid>

</StackPanel>

</Grid>

</UserControl>

MainPage.xaml.cs文件代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

namespace SilverlightClient

{

public partial class MainPage : UserControl

{

public MainPage()

{

InitializeComponent();

this.Loaded += new RoutedEventHandler(MainPage_Loaded);

}

void MainPage_Loaded(object sender, RoutedEventArgs e)

{

Feature ft = new Feature();

Feature ftRoot = new Feature();

ftRoot.FeatureName = "公司部门";

ftRoot.Description = "公司各部门的结构";

ftRoot.Icon = "Icon/l1.png";

Feature ftChild1 = new Feature();

ftChild1.FeatureName = "建筑部";

ftChild1.Description = "负责公司的工程项目";

ftChild1.Icon = "Icon/l2.png";

Feature ftChild1_1 = new Feature();

ftChild1_1.FeatureName = "设计科";

ftChild1_1.Description = "负责项目的设计";

ftChild1_1.Icon = "Icon/l3.png";

Feature ftChild1_2 = new Feature();

ftChild1_2.FeatureName = "工程科";

ftChild1_2.Description = "负责项目的具体实施";

ftChild1_2.Icon = "Icon/l3.png";

ftChild1.Subcomponents.Add(ftChild1_1);

ftChild1.Subcomponents.Add(ftChild1_2);

Feature ftChild2 = new Feature();

ftChild2.FeatureName = "管理部";

ftChild2.Description = "负责管理公司的财务与人事";

ftChild2.Icon = "Icon/l2.png";

Feature ftChild2_1 = new Feature();

ftChild2_1.FeatureName = "财务科";

ftChild2_1.Description = "负责公司的对内对外的财务事宜";

ftChild2_1.Icon = "Icon/l3.png";

Feature ftChild2_2 = new Feature();

ftChild2_2.FeatureName = "总务人事科";

ftChild2_2.Description = "负责公司日常事务及员工招聘";

ftChild2_2.Icon = "Icon/l3.png";

ftChild2.Subcomponents.Add(ftChild2_1);

ftChild2.Subcomponents.Add(ftChild2_2);

ftRoot.Subcomponents.Add(ftChild1);

ftRoot.Subcomponents.Add(ftChild2);

ft.Subcomponents.Add(ftRoot);

tvFeature.ItemsSource = ft.Subcomponents;

}

}

作者:Kinglee
文章出处:Kinglee’s Blog (http://www.cnblogs.com/Kinglee/)
版权声明:本文的版权归作者与博客园共有。转载时须注明本文的详细链接,否则作者将保留追究其法律责任。

有关Silverlight TreeView组件的研究[3]——Silverlight学习笔记(8)相关推荐

  1. 有关Silverlight TreeView组件的研究[2]——Silverlight学习笔记(7)

    二.带复选框的TreeView 说明:在TreeView中设置复选框是十分常见的,这有助于我们对于同组数据的一次性选取或取消.本文就将为你介绍怎样在Silverlight中实现带有Checkbox的T ...

  2. Vue学习(动态组件、组件路由缓存keepalive)-学习笔记

    文章目录 Vue学习(动态组件.组件路由缓存keepalive)-学习笔记 动态组件 组件路由缓存keepalive Vue学习(动态组件.组件路由缓存keepalive)-学习笔记 动态组件 < ...

  3. 有关Silverlight TabControl组件的研究——Silverlight学习笔记(5)

    说明:通过对于Silverlight TabControl组件及其子组件TabItem的学习,您将了解到该组件能够有效充分地利用屏幕空间,并且能展示大量的数据内容,使得应用程序的内容布局更趋合理. [ ...

  4. 四大组件之Service的再学习笔记

    Android中的进程优先级: 进程优先级仅仅是供Android虚拟机查看的,进程优先级越低, 那么在Android运行时若内存趋于阈值时,将会先被杀掉. 当android管理的内存接近阀值时会杀掉一 ...

  5. kendo treeview 修改节点显示值_VBA学习笔记60-1: Treeview控件

    学习资源:<Excel VBA从入门到进阶>第60集 by兰色幻想 本节讲Treeview控件. TreeView控件是以树形结构显示数据的控件.利用TreeView控件,可以设计出树形结 ...

  6. 怎么样玩转信息研究方法指南学习笔记

    本文是基于<怎样玩转信息研究方法指南>所写的个人的阅读总结,该书采用漫画方式进行叙述,十分直观易懂,我在阅读过程中整理笔记,在此基础上进行压缩总结,整理成本文同时,发布于个人博客https ...

  7. Blockchain -Corda框架研究一 cordapp-example 学习笔记

    Corda是Blockchain企业框架之一. 相关链接:https://docs.corda.net/ 国内:https://cncorda.com/ 这周开始准备学习Corda, 首先先研究一下h ...

  8. Mini 容器学习笔记4——组件的生命周期(应用篇)

    Mini容器支持6中生命周期类型: 1. Singleton :单利类型(缺省组件都是单利类型的生命周期,由容器进行托管的) [Test]public void SingletonLifestyleT ...

  9. Mini 容器学习笔记6——组件的获取(应用)

    1. 通过组件Id获取组件实例: [Test]public void GetByIdTest(){ServiceRegistry.Register<Person>("person ...

最新文章

  1. 舒工给您娓娓道来:2019-nCoV,教室布局筛查追溯系统算法解密!
  2. 细粒度图像分割 (FGIS)
  3. tcp/ip详解--封装
  4. 笔记-中项案例题-2018年上-整体管理
  5. JFreeChart插件
  6. Linux Man手册的使用示例
  7. 计算机的英语句子,唯美英语短句
  8. Tensorflow实现数据分档操作
  9. php error_log记录日志的使用方法和配置
  10. Windows10安装Gooey
  11. 哈工大网络安全实验五报告
  12. 全网最全清理c盘大全
  13. AI魔法手!用算法修复老照片
  14. 重庆的flash游戏开发团队
  15. linux eqep驱动框架,【活动结束】《基于28377的伺服驱动系统调试》第一期/共......
  16. flex:1是什么意思
  17. MANIFEST.MF是个什么?
  18. 视频教程-基于Java的WebSocket的聊天室-Java
  19. TeamViewer 连接问题
  20. 企业邮箱哪个好,公司邮箱品牌如何选择?

热门文章

  1. gt designer2不能初始化字体管理器_Windows Terminal 1.1预览版发布:新增字体粗细、随开机启动等功能...
  2. mysql error 1790_Mysql 数据恢复报错
  3. java io字符流_Java IO流字符流简介及基本使用
  4. 金融数据分析与挖掘实战1.4.1-1.4.3
  5. 程序员:凭什么他大专12K,而我硕士研究生才5K?
  6. Qt_发送邮件(以qq邮箱为例)
  7. wireshark 开始抓包
  8. sts 的js代码不变色_[黑科技] 使用 Laravel Livewire 来构建实时搜索功能(不使用一行 JS 代码)...
  9. html页面图片翻转特效代码,如何使用css实现翻转图片的效果(附代码)
  10. oracle数据库等级,[数据库]Oracle数据库建表并用SQL编程分等级