Silverlight 中 ComboBox+TreeView 实现的下拉控件

性能描述:

  ComboBox+TreeView实现的用户控件,设置数据源后,可以递归加载数据,支持双向绑定

  控件截图:

  

 

  XAML界面设计:

<UserControl x:Class="ChuanyeOA.CustomControls.ComboBoxTree"
    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=
"30" d:DesignWidth="300" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
    
    <Grid x:Name="LayoutRoot">
        <ComboBox Height="23" Name="cbMain" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" DropDownOpened="cbMain_DropDownOpened" DropDownClosed="cbMain_DropDownClosed">
            <ComboBoxItem x:Name="cbItemTreeView">
                <ComboBoxItem.Content>
                    <sdk:TreeView Name="tvList" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" SelectedItemChanged="tvList_SelectedItemChanged" />
                </ComboBoxItem.Content>
            </ComboBoxItem>

            <ComboBoxItem x:Name="cbItemDisplay" >
            </ComboBoxItem>
        </ComboBox>
    </Grid>
</UserControl>

  后台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;

using System.Windows.Data;

using System.Collections;
using System.Reflection;

/********************************
 * 创建人:刘跃飞
 * 创建时间:2010-09-21
 * 功能描述:ComboBox+TreeView实现的用户控件,主要实现选择TreeView中的项做为ComboBox的项的功能
 * 属性描述:此控件包含以下几个属性
 * 1.ItemsSource:控件要绑定的数据源
 * 2.SelectedValuePath:选中的值的属性
 * 3.DisplayMemberPath:获取或设置为每个数据项要显示的属性的名称
 * 4.SelectedItem  当前选择的项
 * 5.SelectedValue  当前选择的值
 * 6.IsRecursionEnabled 设置是否启用递归加载数据源中的项 。需要设置ChildName和ParentName两个属性
 * 7.ChildMemberPath 递归加载数据源时,实体中的子级属性项
 * 8. ParentMemberPath 递归加载数据源时,实体中的父级属性项
 * 9.IsExpandAll 设置TreeView打开时是全部打开状态还是收缩状态 默认为True打开状态
 * 
 * 使用方法:
 * 注意事项:本控件设计尚不完善,【ItemsSource】属性需要在最后设置,否则,将提示错误
 * 1.界面上直接设置,例如:
 *  <myControl:ComboBoxTree SelectedValue="{Binding DepartmentID, Mode=TwoWay}" DisplayMemberPath="DepartName" SelectedValuePath="ID" IsRecursionEnabled="True" ChildMemberPath="ID" ParentMemberPath="ParentID"  ItemsSource="{Binding Data, Source={StaticResource a_DepartmentDomainDataSource}}">
 * 2.在后台代码中直接设置,例如:
 *           comboBoxTree1.DisplayMemberPath = "Name";
 *           comboBoxTree1.SelectedValuePath = "ID";
 *           comboBoxTree1.ChildMemberPath = "ID";
 *           comboBoxTree1.ParentMemberPath = "ParentID";
 *           comboBoxTree1.IsRecursionEnabled = true;
 *           comboBoxTree1.ItemSource = MyControls.DepartmentInfo.GetDepartment().AsEnumerable();
 * 3.如果启用递归加载,父级ID为【0】的项,将作为根节点加载,如果没有父级ID为【0】的项,将无法进行加载
 * 4.此控件用到了TreeView的ExpandAll扩展方法,需要添加【System.Windows.Controls.Toolkit】类库的引用,否则将提示找不到此方法
 * ***********************************/


namespace ChuanyeOA.CustomControls
{
    public partial class ComboBoxTree : UserControl
    {      
        public ComboBoxTree()
        {
            InitializeComponent();

            this.LayoutUpdated += new EventHandler(ComboBoxTree_LayoutUpdated);
        }

        void ComboBoxTree_LayoutUpdated(object sender, EventArgs e)
        {
            if (this.IsExpandAll)
            {
                //--展开所有节点
                tvList.ExpandAll();
            }
        }

        #region ItemSource 设置或获得绑定的数据源(属性)
        
        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource"typeof(IEnumerable), typeof(ComboBoxTree), new PropertyMetadata(new PropertyChangedCallback(ItemsSourcePropertyChangedCallBack)));
        /// <summary>
        /// 数据源
        /// </summary>
        public IEnumerable ItemsSource
        {
            get
            {
                return (IEnumerable)this.GetValue(ItemsSourceProperty);
            }
            set
            {
                this.SetValue(ItemsSourceProperty, value);
            }
        }
        //--属性更改的回调事件
        public static void ItemsSourcePropertyChangedCallBack(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (sender != null)
            {
                ComboBoxTree comboBoxTree = sender as ComboBoxTree;

                if (comboBoxTree.IsRecursionEnabled == false)
                {
                    // 以列表的方式加载数据
                    comboBoxTree.LoadTreeViewDataWithList();
                }
                else
                {
                    //以递归的方式加载数据
                    comboBoxTree.LoadTreeViewDataWithRecursion();
                }

            }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值