博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CustomMultipleDropDownBox
阅读量:5886 次
发布时间:2019-06-19

本文共 10575 字,大约阅读时间需要 35 分钟。

cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Collections;using System.Windows.Data;using System.Reflection;using System.Collections.ObjectModel;using System.Windows.Controls.Primitives;using System.ComponentModel;namespace GEMS.Windows.Controls.CustomControls{    public class CustomMultipleDropDownBox : ComboBox    {        #region Property        protected ListBox MyListBox        {            get            {                return base.GetTemplateChild("MyListBox") as ListBox;            }        }        protected ListBox MySelectedListBox        {            get            {                return base.GetTemplateChild("MySelectedListBox") as ListBox;            }        }        public IList SelectedItems        {            get { return (IList)GetValue(SelectedItemsProperty); }            set { SetValue(SelectedItemsProperty, value); }        }        // Using a DependencyProperty as the backing store for SelectedItems1.  This enables animation, styling, binding, etc...        public static readonly DependencyProperty SelectedItemsProperty =            DependencyProperty.Register("SelectedItems", typeof(IList), typeof(CustomMultipleDropDownBox), new PropertyMetadata(null, new PropertyChangedCallback(OnSelectedItemsChanged)));        public ListSortDirection Sort        {            get { return (ListSortDirection)GetValue(SortProperty); }            set { SetValue(SortProperty, value); }        }        // Using a DependencyProperty as the backing store for Sort.  This enables animation, styling, binding, etc...        public static readonly DependencyProperty SortProperty =            DependencyProperty.Register("Sort", typeof(ListSortDirection), typeof(CustomMultipleDropDownBox), new PropertyMetadata(ListSortDirection.Ascending));        public string SortMemberPath        {            get { return (string)GetValue(SortMemberPathProperty); }            set { SetValue(SortMemberPathProperty, value); }        }        // Using a DependencyProperty as the backing store for SortMemberPath.  This enables animation, styling, binding, etc...        public static readonly DependencyProperty SortMemberPathProperty =            DependencyProperty.Register("SortMemberPath", typeof(string), typeof(CustomMultipleDropDownBox), new PropertyMetadata(null));                    #endregion        #region construtor        static CustomMultipleDropDownBox()        {            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomMultipleDropDownBox), new FrameworkPropertyMetadata(typeof(CustomMultipleDropDownBox)));        }        public CustomMultipleDropDownBox()        {                       this.Loaded += CustomMultipleDropDownBox_Loaded;            // this.SelectedItems.CollectionChanged += SelectedItems_CollectionChanged;        }        void SelectedItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)        {        }        void CustomMultipleDropDownBox_Loaded(object sender, RoutedEventArgs e)        {            //if (this.SelectedItems == null)            //{            //    Type type = this.SelectedItems.GetType();            //    ConstructorInfo constructor = type.GetConstructor(new Type[0]);            //    //使用构造器对象来创建对象            //    object obj = constructor.Invoke(new Object[0]);            //    // this.SelectedItems = constructor.Invoke(new Object[0]);            //}        }        public override void OnApplyTemplate()        {            base.OnApplyTemplate();            //load the text box control            if (this.MyListBox != null)            {                                    //this.MyListBox.SelectionChanged += MyListBox_SelectionChanged;                         //DataTemplate dt1 = new DataTemplate();                //FrameworkElementFactory fef1 = new FrameworkElementFactory(typeof(CheckBox));                //Binding binding1 = new Binding();                //binding1.Path = new PropertyPath("Tag");                //binding1.Mode = BindingMode.TwoWay;                //binding1.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;                //binding1.RelativeSource = RelativeSource.Self;                //Binding binding2 = new Binding();                //binding2.Path = new PropertyPath(this.DisplayMemberPath);                //binding2.Mode = BindingMode.TwoWay;                //binding2.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;                //fef1.SetBinding(CheckBox.IsCheckedProperty, binding1);                //fef1.SetBinding(CheckBox.ContentProperty, binding2);                //fef1.SetValue(CheckBox.WidthProperty, this.Width);                //fef1.SetValue(CheckBox.TagProperty, false);                ////fef1.SetValue(CheckBox.NameProperty, "cBoxSelected");                //fef1.AddHandler(CheckBox.UncheckedEvent, (RoutedEventHandler)cBoxSelected_Unchecked, true);                //fef1.AddHandler(CheckBox.CheckedEvent, (RoutedEventHandler)cBoxSelected_Checked, true);                //dt1.VisualTree = fef1;                //this.MyListBox.ItemTemplate = dt1;            }            if (this.MySelectedListBox != null)            {                this.MySelectedListBox.Items.SortDescriptions.Add(new SortDescription(string.IsNullOrWhiteSpace(this.SortMemberPath) ? this.DisplayMemberPath : this.SortMemberPath, this.Sort));            }        }               protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)        {            this.Items.SortDescriptions.Add(new SortDescription(string.IsNullOrWhiteSpace(this.SortMemberPath) ? this.DisplayMemberPath : this.SortMemberPath, this.Sort));            base.OnItemsSourceChanged(oldValue, newValue);            if (newValue!=null)            {                foreach (var item in this.Items)                {                    // string DisplayName = item.GetType().GetProperty(this.DisplayName).GetValue(item, null).ToString();                    Binding binding = new Binding(this.DisplayMemberPath) { Source = item };                    Binding binding1 = new Binding();                    binding1.Path = new PropertyPath("Tag");                    binding1.Mode = BindingMode.TwoWay;                    binding1.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;                    binding1.RelativeSource = RelativeSource.Self;                    CheckBox cb = new CheckBox();                    cb.Width = this.Width;                    if (this.SelectedItems!=null&&this.SelectedItems.Contains(item))                    {                        cb.Tag = true;                    }                    else                    {                        cb.Tag = false;                    }                    cb.SetBinding(CheckBox.ContentProperty, binding);                    cb.SetBinding(CheckBox.IsCheckedProperty, binding1);                    cb.DataContext = item;                    //cb.Content = DisplayName;                    cb.Checked += cBoxSelected_Checked;                    cb.Unchecked += cBoxSelected_Unchecked;                    this.MyListBox.Items.Add(cb);                }                       }                       //this.SelectedItems = Activator.CreateInstance(newValue.GetType());            //Type type = this.SelectedItems.GetType();            //ConstructorInfo constructor = type.GetConstructor(new Type[0]);            ////使用构造器对象来创建对象            //object obj = constructor.Invoke(new Object[0]);            //this.SelectedItems = constructor.Invoke(new Object[0]);        }        private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)        {            //CustomMultipleDropDownBox cmd = d as CustomMultipleDropDownBox;            //cmd.SelectedDisplayNames = new List
(); //foreach (var item in cmd.SelectedItems) //{ // cmd.SelectedDisplayNames.Add(new DisplayName() { Name = item.GetType().GetProperty(cmd.DisplayMemberPath).GetValue(item, null).ToString() }); //} ////if (e.NewValue!=null&&cmd.ItemsSource!=null) ////{ ////} //cmd.SelectedDisplayNames = cmd.SelectedDisplayNames.OrderBy(p=>p.Name).ToList(); } #endregion #region Event void MyListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { // this.MySelectedListBox.ItemsSource = this.MyListBox.SelectedItems; } private void cBoxSelected_Unchecked(object sender, RoutedEventArgs e) { if (this.SelectedItems == null) { this.SelectedItems = new ObservableCollection
(); } CheckBox cb = sender as CheckBox; //cb.Tag = false; this.SelectedItems.Remove(cb.DataContext); } private void cBoxSelected_Checked(object sender, RoutedEventArgs e) { if (this.SelectedItems == null) { this.SelectedItems = new ObservableCollection(); } CheckBox cb = sender as CheckBox; //cb.Tag = true; this.SelectedItems.Add(cb.DataContext); } #endregion } }
View Code

 

style

-->
View Code

 

 

转载于:https://www.cnblogs.com/FaDeKongJian/archive/2013/05/25/3099117.html

你可能感兴趣的文章
基于WeiPHP的微信批量文本自定义回复的快捷实现
查看>>
ps动态文字图片
查看>>
android之自定义ViewGroup和自动换行的布局的实现
查看>>
系统化思维导论读书笔记
查看>>
storm使用问题随笔
查看>>
我的友情链接
查看>>
Office2007另存为显示“正在初始化要显示的根文件夹”解决方法
查看>>
我的友情链接
查看>>
Win8大百科06期:硬件需求/版本知多少?
查看>>
raid
查看>>
我的友情链接
查看>>
hub-spoke Dynamic site-to-site peer and ××× Cliens
查看>>
十个1分钟换来健康,搞IT必看
查看>>
学好Java的10个建议
查看>>
mysql中的数据导入导出
查看>>
Java脚本:去除字符串中空值
查看>>
Open edX课程数据的存储方式
查看>>
将bean中的数据复制到map中
查看>>
https方式使用git@osc设置密码的方式
查看>>
种一颗树最好的时间是十年前,而后是现在
查看>>