Silverlight TreeView 带 checkbox和图片

本文介绍如何在Silverlight中创建带有复选框和图片的树视图控件,并提供两种不同样式的实现方案。第一种方案使用HierarchicalDataTemplate进行层级数据绑定,第二种方案则通过自定义样式实现。

前段时间做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>
View Code

后台代码

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;
    }
}
View Code

第二种效果图如下

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>
View Code

后台代码

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
    }
}
View Code
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;
        }
    }
}
View Code

 

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

 

转载于:https://www.cnblogs.com/ZJ199012/p/4024058.html

(1)普通用户端(全平台) 音乐播放核心体验: 个性化首页:基于 “听歌历史 + 收藏偏好” 展示 “推荐歌单(每日 30 首)、新歌速递、相似曲风推荐”,支持按 “场景(通勤 / 学习 / 运动)” 切换推荐维度。 播放页功能:支持 “无损音质切换、倍速播放(0.5x-2.0x)、定时关闭、歌词逐句滚动”,提供 “沉浸式全屏模式”(隐藏冗余控件,突出歌词专辑封面)。 多端同步:自动同步 “播放进度、收藏列表、歌单” 至所有登录设备(如手机暂停后,电脑端打开可继续播放)。 音乐发现管理: 智能搜索:支持 “歌曲名 / 歌手 / 歌词片段” 搜索,提供 “模糊匹配(如输入‘晴天’联想‘周杰伦 - 晴天’)、热门搜索词推荐”,结果按 “热度 / 匹配度” 排序。 歌单管理:创建 “公开 / 私有 / 加密” 歌单,支持 “批量添加歌曲、拖拽排序、一键分享到社交平台”,系统自动生成 “歌单封面(基于歌曲风格配色)”。 音乐分类浏览:按 “曲风(流行 / 摇滚 / 古典)、语言(国语 / 英语 / 日语)、年代(80 后经典 / 2023 新歌)” 分层浏览,每个分类页展示 “TOP50 榜单”。 社交互动功能: 动态广场:查看 “关注的用户 / 音乐人发布的动态(如‘分享新歌感受’)、好友正在听的歌曲”,支持 “点赞 / 评论 / 转发”,可直接点击动态中的歌曲播放。 听歌排行:个人页展示 “本周听歌 TOP10、累计听歌时长”,平台定期生成 “全球 / 好友榜”(如 “好友中你本周听歌时长排名第 3”)。 音乐圈:加入 “特定曲风圈子(如‘古典音乐爱好者’)”,参 “话题讨论(如‘你心中最经典的钢琴曲’)、线上歌单共创”。 (2)音乐人端(创作者中心) 作品管理: 音乐上传:支持 “无损音频(FLAC/WAV)+ 歌词文件(LRC)+ 专辑封面” 上传,填写 “歌曲信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值