自定义ComboBox控件,完美解决C#自带的ComboBox效率慢的问题
欢迎关注http://blog.csdn.net/aaa123524457
转载请注明出处:http://blog.csdn.net/aaa123524457/article/details/47058675
在做项目的时候,用到了ComboBox来做下拉列表的功能;但是在用的时候发现C#自带的ComboBox效率非常的慢!
当然我添加的是股票、期货的合约数据,大概有几千条信息,算是比较大的。如果用到的数据很少,就可以直接用C#自带的。
C#自带的combobox之所以慢,是因为内部做了大量的转型、匹配工作。具体这里就不分析了。有兴趣的朋友可以去找找资料研究一下。
我这里用了WPF做了一个自定义combobox控件。自己写了一个和C#自带的combobox一样功能的空间,但是效率却非常快!废话不多说,直接看代码:
欢迎关注http://blog.csdn.net/aaa123524457
转载请注明出处:http://blog.csdn.net/aaa123524457/article/details/47058675
前台代码:
<UserControl x:Class=" MyComboBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" Loaded="UserControl_Loaded" > 欢迎关注http://blog.csdn.net/aaa123524457 转载请注明出处:http://blog.csdn.net/aaa123524457/article/details/47058675 <Grid> <DockPanel> <Border DockPanel.Dock="Top" BorderBrush="Black" <StackPanel Orientation="Horizontal"> <TextBox Margin="0,0,1,0" Name="textBox_ComboBox" <Button Width="13" Name="button_drop" </StackPanel> </Border> <ListBox DockPanel.Dock="Top" Margin="0,1,0,0" </DockPanel> </Grid> </UserControl> |
欢迎关注http://blog.csdn.net/aaa123524457
转载请注明出处:http://blog.csdn.net/aaa123524457/article/details/47058675
后台代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace MyComboBox_real { ///<summary> /// Interaction logic for MyComboBox.xaml ///重写的一具有部分combobox功能¨1能¨1的ì?mycombobox,效?ì率¨o比ਨ原-combobox高?很¨1多¨¤倍à?!ê? ///可¨|扩¤?充? ///</summary> public { #region 内¨2部?类¤¨¤ ///<summary> /// Items存??储??é类¤¨¤ ///</summary> public { #region 重?要°a变à?量¢? ///<summary> ///存??储??é传??递ìY进?来¤??的ì?string类¤¨¤型¨a的ì?数oy据Y ///</summary> private ///<summary> ///存??储??é传??递ìY进?来¤??的ì?string类¤¨¤型¨a的ì?数oy据Y ///</summary> public { get { set { } #endregion #region 构1造¨?函?¥数oy ///<summary> ///无T参?构1造¨?函?¥数oy ///</summary> public MyItems() { } #endregion 欢迎关注http://blog.csdn.net/aaa123524457 转载请注明出处:http://blog.csdn.net/aaa123524457/article/details/47058675 #region 业°|ì务?处?|理¤¨a函?¥数oy ///<summary> ///返¤|ì回?与??输o?入¨?字á?符¤?串??相¨¤似?的ì?所¨′有?D字á?符¤?串?? ///</summary> ///<param name="msg"></param> ///<returns></returns> public { return ( f => ((f.Length >= msg.Length) && (((f.Substring(0, msg.Length)).ToLower()) == (msg.ToLower()))) ).ToList<string>(); } ///<summary> ///添?¨a加¨?string类¤¨¤型¨a的ì?数oy据Y ///</summary> ///<param name="msg">数oy据Y</param> ///<returns>成¨|功|返¤|ì回?1</returns> public { this.ListItemMSG.Add(msg); return 1; } ///<summary> ///清?除yitems ///</summary> ///<returns></returns> public { this.ListItemMSG.Clear(); return 1; } #endregion } #endregion #region 重?要°a变à?量¢? ///<summary> ///项?目?处?|理¤¨a ///</summary> public #endregion #region 构1造¨?函?¥数oy ///<summary> ///无T参?构1造¨?函?¥数oy ///</summary> public MyComboBox() { InitializeComponent(); //this.listBox_ComboBox.ItemsSource = this.myItems.getMyItems(); } #endregion #region 业°|ì务?处?|理¤¨a函?¥数oy ///<summary> ///监¨¤控?键¨1盘¨?操¨′作á?? ///</summary> ///<param name="sender"></param> ///<param name="e"></param> private { TextBox tb = senderas if (tb == { return; } // 键¨1盘¨?“??↑¨1”?à、?é“??←?”?à键¨1 if ((e.Key == { if (this.listBox_ComboBox.SelectedIndex > 0) { this.listBox_ComboBox.SelectedIndex--; } } // 键¨1盘¨?“??↓y”?à、?é“??→¨2”?à键¨1 else { if (this.listBox_ComboBox.SelectedIndex < (this.listBox_ComboBox.Items.Count - 1)) { this.listBox_ComboBox.SelectedIndex++; } } // 回?车|ì键¨1 else { tb.Text = this.listBox_ComboBox.SelectedItem ==null ? tb.Text : this.listBox_ComboBox.Visibility =Visibility.Collapsed; } else { if ((tb.Text != { if (this.myItems.getMatchDatas(tb.Text).Count > 0) { this.listBox_ComboBox.ItemsSource =this.myItems.getMatchDatas(tb.Text); this.listBox_ComboBox.Visibility =Visibility.Visible; } else { this.listBox_ComboBox.Visibility =Visibility.Collapsed; } } else { this.listBox_ComboBox.Visibility =Visibility.Collapsed; } } // 重?置?光a标à¨o位?置? tb.Select(tb.Text.Length, 1); } ///<summary> ///选?择?内¨2容¨Y ///</summary> ///<param name="sender"></param> ///<param name="e"></param> private { ListBox lb = senderas if (lb == { return; } if (lb.SelectedItem ==null) { lb.Visibility = Visibility.Collapsed; return; } this.textBox_ComboBox.Text = lb.SelectedItem.ToString(); lb.Visibility = Visibility.Collapsed; // 重?置?光a标à¨o位?置? this.textBox_ComboBox.Select(this.textBox_ComboBox.Text.Length, 1); } #endregion 欢迎关注http://blog.csdn.net/aaa123524457 转载请注明出处:http://blog.csdn.net/aaa123524457/article/details/47058675 #region 私?有?D函?¥数oy ///<summary> ///重?置?控?件t宽¨a度¨¨ ///</summary> ///<param name="sender"></param> ///<param name="e"></param> private { this.textBox_ComboBox.Width =this.ActualWidth - 1 - } ///<summary> ///模?ê拟a点ì?击??下?拉¤- ///</summary> ///<param name="sender"></param> ///<param name="e"></param> private { if (this.listBox_ComboBox.Visibility ==Visibility.Visible) { this.listBox_ComboBox.Visibility =Visibility.Collapsed; } else { this.listBox_ComboBox.ItemsSource =this.myItems.ListItemMSG; this.listBox_ComboBox.Visibility =Visibility.Visible; } } #endregion } } |
欢迎关注http://blog.csdn.net/aaa123524457
转载请注明出处:http://blog.csdn.net/aaa123524457/article/details/47058675
效果图:
欢迎关注http://blog.csdn.net/aaa123524457
转载请注明出处:http://blog.csdn.net/aaa123524457/article/details/47058675
需要注意的一点是:如果是在winform里面添加这个自定义控件,需要稍微把自定义控件的高度拉长一点,不然会被遮住的哦;如果在wpf里面引用就无所谓了。
版权声明:本文为博主原创文章,未经博主允许不得转载。