Silverlight TreeView 带 checkbox和图片

前段时间做Silverlight TreeView 控件,但是要带checkbox和图片,在网上到处找相关的例子,效果图如下

xaml代码

<UserControl x:Class="SlmenuTest.Tree"
    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:common="clr-namespace:System.Windows;assembly=System.Windows.Controls"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

    <UserControl.Resources>
        <common:HierarchicalDataTemplate x:Key="Level3Template">
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Icon}" Width="16" Height="16"/>
                <CheckBox />
                <TextBlock Text="{Binding Name}"  Foreground="Black" />
            </StackPanel>
        </common:HierarchicalDataTemplate>

        <common:HierarchicalDataTemplate x:Key="Level2Template" ItemsSource="{Binding Level3s}" ItemTemplate="{StaticResource Level3Template}">
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Icon}" Width="16" Height="16"/>
                <CheckBox />
                <TextBlock Text="{Binding Name}" Foreground="Green"/>
            </StackPanel>
        </common:HierarchicalDataTemplate>

        <common:HierarchicalDataTemplate x:Key="Level1Template" ItemsSource="{Binding Level2s}" ItemTemplate="{StaticResource Level2Template}">
            <StackPanel  Orientation="Horizontal">
                <Image Source="{Binding Icon}" />
                <CheckBox />
                <TextBlock Foreground="Blue" Text="{Binding Name}"/>
            </StackPanel>
        </common:HierarchicalDataTemplate>
    </UserControl.Resources>

后台代码

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;
using System.Collections.ObjectModel;

namespace SlmenuTest
{
    public partial class Tree : UserControl
    {

        public Tree()
        {
            InitializeComponent();
            InitTreeData();
        }

        #region Data
        private void InitTreeData()
        {
            myTree.ItemsSource = new ObservableCollection<Level1>
            {
                new Level1
                {
                    Name = "test1",
                    Icon="../images/default.png",
                    Level2s =
                    {
                        new Level2
                        {
                            Name="基础信息",
                            Icon = "../images/search.png",
                            Level3s =
                            {
                                new Level3 { Name = "类别" ,Icon = "../images/computer.png"},
                                new Level3 { Name = "部门" ,Icon = "../images/computer_on.png"},
                                new Level3 { Name = "类别2" ,Icon = "../images/exit.png"},
                                new Level3 { Name = "部门2" ,Icon = "../images/edit.png"}
                            }
                        },
                        new Level2
                        {
                            Name="扩展信息",
                            Icon="../images/search.png"
                        }

                    }
                },
                new Level1
                {
                    Name="test2",
                    Icon="../images/default.png",
                    Level2s=
                    {
                        new Level2
                        {
                            Name="报表管理",
                            Icon = "../images/search.png",
                            Level3s =
                            {
                                new Level3 { Name = "报表1" ,Icon = "../images/default.png"},
                                new Level3 { Name = "报表2" ,Icon = "../images/default.png"},
                                new Level3 { Name = "报表3" ,Icon = "../images/default.png"},
                                new Level3 { Name = "报表4",Icon="../images/default.png"}
                            }
                        }
                    }
                },
                new Level1
                {
                    Name="test3",
                    Icon="../images/default.png",
                    Level2s=
                    {
                        new Level2
                        {
                            Name="系统管理",
                            Icon = "../images/search.png",
                            Level3s =
                            {
                                new Level3 { Name = "权限设置" ,Icon = "../images/default.png"},
                                new Level3 { Name = "用户管理" , Icon = "../images/default.png"}
                            }
                        }
                    }
                }

            };
        }
        #endregion

        private void myTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeViewItem item = e.NewValue as TreeViewItem;

        }

    }

    public class Level1
    {
        public Level1()
        {
            Level2s = new ObservableCollection<Level2>();
        }
        public string Name { get; set; }
        public string Icon { get; set; }
        public ObservableCollection<Level2> Level2s { get; set; }
    }

    public class Level2
    {
        public Level2()
        {
            Level3s = new ObservableCollection<Level3>();
        }

        public string Name { get; set; }
        public string Icon { get; set; }
        public ObservableCollection<Level3> Level3s { get; set; }
    }

    public class Level3
    {
        public string Name { get; set; }
        public string Icon { get; set; }
        //public event EventHandler click;
    }
}

第二种效果图如下

xaml代码如下

<UserControl x:Class="SLColorPickerDemo.Tree"
    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"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

    <UserControl.Resources>
        <Style x:Key="RedItemStyle" TargetType="sdk:TreeViewItem">
            <Setter Property="HeaderTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Image Source="images/default.png"/>
                            <CheckBox />
                            <TextBlock Text="{Binding}" Foreground="Red" FontStyle="Italic" />
                        </StackPanel>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="IsExpanded" Value="False" />
        </Style>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="White">
        <sdk:TreeView Margin="5" Grid.Column="0" Grid.Row="1" Name="tvTree" SelectedItemChanged="tvTree_SelectedItemChanged" />
        <Border BorderBrush="Gray" BorderThickness="1" Padding="8" Margin="5">
            <StackPanel x:Name="DetailsPanel" Margin="4" Width="155">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="版块ID: " FontWeight="Bold"  />
                    <TextBlock Text="{Binding ForumID}" />
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="版块名称: " FontWeight="Bold"  />
                    <TextBlock Text="{Binding ForumName}" />
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="版块信息: " FontWeight="Bold" />
                    <TextBlock x:Name="DetailText" TextWrapping="Wrap" Text="{Binding ForumName}"/>
                </StackPanel>
            </StackPanel>
        </Border>
    </Grid>
</UserControl>

后台代码

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace SLColorPickerDemo
{
    public class ForumInfo : INotifyPropertyChanged
    {
        public int ForumID { get; set; }
        public int ParendID { get; set; }
        public string ForumName { get; set; }

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        private void PropertyChaged(string propertyName)
        {
            PropertyChangedEventHandler handle = PropertyChanged;
            if (handle != null)
                handle.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}

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;
using System.Collections.ObjectModel;

namespace SLColorPickerDemo
{
    public partial class Tree : UserControl
    {

        List<ForumInfo> forumList = new List<ForumInfo>();

        public Tree()
        {
            InitializeComponent();
            forumList = GetForumData();
            AddTreeNode(0, null);
        }

        private void AddTreeNode(int parentID, TreeViewItem treeViewItem)
        {
            List<ForumInfo> result = (from forumInfo in forumList
                                      where forumInfo.ParendID == parentID
                                      select forumInfo).ToList<ForumInfo>();

            if (result.Count > 0)
            {
                foreach (ForumInfo foruminfo in result)
                {
                    TreeViewItem objTreeNode = new TreeViewItem();
                    objTreeNode.Header = foruminfo.ForumName;
                    objTreeNode.DataContext = foruminfo;

                    objTreeNode.ItemContainerStyle = this.Resources["RedItemStyle"] as Style;
                    //添加根节点
                    if (treeViewItem == null)
                    {
                        tvTree.Items.Add(objTreeNode);
                        //tvTree.ItemContainerStyle = this.Resources["TreeStyle"] as Style;
                    }
                    else
                    {
                        treeViewItem.Items.Add(objTreeNode);
                    }
                    AddTreeNode(foruminfo.ForumID, objTreeNode);
                }
            }
        }

        public List<ForumInfo> GetForumData()
        {
            List<ForumInfo> forumList = new List<ForumInfo>();
            forumList.Add(new ForumInfo() { ForumID = 1, ParendID = 0, ForumName = "笔记本版块" });
            forumList.Add(new ForumInfo() { ForumID = 2, ParendID = 0, ForumName = "台式机版块" });

            forumList.Add(new ForumInfo() { ForumID = 3, ParendID = 1, ForumName = "Dell笔记本" });
            forumList.Add(new ForumInfo() { ForumID = 4, ParendID = 1, ForumName = "IBM笔记本" });
            forumList.Add(new ForumInfo() { ForumID = 5, ParendID = 4, ForumName = "IBM-T系列" });
            forumList.Add(new ForumInfo() { ForumID = 6, ParendID = 4, ForumName = "IBM-R系列" });

            forumList.Add(new ForumInfo() { ForumID = 7, ParendID = 2, ForumName = "联想台式机" });
            forumList.Add(new ForumInfo() { ForumID = 8, ParendID = 2, ForumName = "方正台式机" });
            forumList.Add(new ForumInfo() { ForumID = 9, ParendID = 2, ForumName = "HP台式机" });
            forumList.Add(new ForumInfo() { ForumID = 10, ParendID = 7, ForumName = "联想家悦H系列" });
            forumList.Add(new ForumInfo() { ForumID = 11, ParendID = 7, ForumName = "联想IdeaCentre系列" });

            return forumList;
        }

        private void tvTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeViewItem item = e.NewValue as TreeViewItem;
            ForumInfo fi = item.DataContext as ForumInfo;

            DetailsPanel.DataContext = fi;
        }
    }
}

时间比较长了参考的文章找不到,找到了再把链接贴上。

时间: 2024-10-24 10:31:35

Silverlight TreeView 带 checkbox和图片的相关文章

WPF 带CheckBox、图标的TreeView

WPF 带CheckBox.图标的TreeView 在WPF实际项目开发的时候,经常会用到带CheckBox的TreeView,虽然微软在WPF的TreeView中没有提供该功能,但是微软在WPF中提供强大的ItemTemplate模板功能和自定义样式,那我们可以自己写一个这样的控件供自己使用. 我自己写的这个比较简单. 首先写一个供TreeView使用的数据模型,并且实现INotifyPropertyChanged接口,用于向客户端(通常是执行绑定的客户端)发出某一属性值已更改的通知,当属性改

【转】带checkbox的ListView实现(二)——自定义Checkable控件的实现方法

原文网址:http://blog.csdn.net/harvic880925/article/details/40475367 前言:前一篇文章给大家展示了传统的Listview的写法,但有的时候我们并不想在DataHolder类中加一个标识是否选中的checked的成员变量,因为在项目开发中,大部分的ListItemLayout布局都是大家共用的,有些人根本不需要checkbox控件,所以会在初始化的时候把这个控件给隐藏掉,但我们的DataHolder在构造的时候以及ListItemAdapt

基于jQuery带标题的图片3D切换焦点图

今天给大家分享一款基于jQuery带标题的图片3D切换焦点图.这款焦点图适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗. 在线预览   源码下载 实现的代码. html代码: <div id="wowslider-container"> <div class="ws_images"> <ul> <li><a href="#"> &

带原点的图片轮播

<html xmlns="http://www.w3.org/1999/xhtml"><head><title>jquery全屏banner自动轮播切换</title><script type="text/javascript" src="/ajaxjs/jquery-1.6.2.min.js"/></script><script type="text/jav

【Android】Android实现自定义带文字和图片的Button

在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法. 一.用系统自带的Button实现 最简单的一种办法就是利用系统自带的Button来实现,这种方式代码量最小.在Button的属性中有一个是drawableLeft,这个属性可以把图片设置在文字的左边,但是这种方式必须让icon的背景色是透明的,如果icon的背景色不是透明的话,会导致点击按钮时icon部分的背景色不会发生变化. 主要代码: <Button android:id="@+id/bt3

带缩略图的图片切换效果

1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8" /> 5 <title></title> 6 <style type="text/css"> 7 *{margin: 0;padding: 0;} 8 9 #box{width: 520px;height: 350px;margin:50px auto;border

在IE7下带链接的图片去除边框问题

在昨天完成的一个项目中,出现了这么一个问题:解决后记录下来,唯恐忘记. 在测试兼容性的时候,发现带链接的图片在IE7下显示有蓝色的边框.后来才知道有以下的解决方案: 在那张图片的html代码中加上"border: 0;"或"border: none;"; [border:0;]把border设为"0"像素虽然在页面上看不见,但按border默认值理解,浏览器依然对border-width/border-color进行了渲染,即已经占用了内存值.

[Ext JS 4] 实战之多选下拉单 (带checkbox) 续 - 带ALL 选项

前言 在 [Ext JS 4] 实战之多选下拉单 (带checkbox) 这一篇中有介绍如何开发带有checkbox 的多选菜单. 但是实际项目开发过程中, 用户的需求也是不断精进的. 使用淘宝或是其他网站购物车功能的用户对全选就特别习惯, 所以他们也希望在下拉单中也能有  "ALL" 这样的选项. 但是Extjs 本身提供的多选下拉单,功能比较有限. 之前有扩充过带 checkbox, 现在又要多扩充一个 "ALL" 选项了. 要求是: 1. 选中"AL

Android实现自定义带文字和图片的Button

在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法. 一.用系统自带的Button实现 最简单的一种办法就是利用系统自带的Button来实现,这种方式代码量最小.在Button的属性中有一个是drawableLeft,这个 属性可以把图片设置在文字的左边,但是这种方式必须让icon的背景色是透明的,如果icon的背景色不是透明的话,会导致点击按钮时icon部分的背景 色不会发生变化. 主要代码: <Button android:id="@+id/b