自定义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" BorderThickness="1"> <StackPanel Orientation="Horizontal"> <TextBox Margin="0,0,1,0" Name="textBox_ComboBox" KeyUp="textBox_ComboBox_KeyUp" /> <Button Width="13" Name="button_drop" HorizontalAlignment="Right" FontSize="7" Click="button_drop_Click">▼</Button> </StackPanel> </Border> <ListBox DockPanel.Dock="Top" Margin="0,1,0,0" Name="listBox_ComboBox" Visibility="Collapsed" MouseDoubleClick="listBox_ComboBox_MouseDoubleClick" /> </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功能¨?能¨?的??mycombobox,效?ì率¨?比?¨¨原-combobox高?很¨?多¨¤倍??!ê? ///可¨?扩¤?充? ///</summary> public partial class MyComboBox : UserControl { #region 内¨?部?类¤¨¤ ///<summary> /// Items存??储??é类¤¨¤ ///</summary> public class MyItems { #region 重?要°a变??量?? ///<summary> ///存??储??é传??递?Y进?来¤??的??string类¤¨¤型¨?的??数?y据Y ///</summary> private List<string> listItemMSG = new List<string>(); ///<summary> ///存??储??é传??递?Y进?来¤??的??string类¤¨¤型¨?的??数?y据Y ///</summary> public List<string> ListItemMSG { get { return this.listItemMSG; } set { this.listItemMSG = value; } } #endregion
#region 构1造¨?函??数?y ///<summary> ///无T参?构1造¨?函??数?y ///</summary> public MyItems() {
} #endregion 欢迎关注http://blog.csdn.net/aaa123524457 转载请注明出处:http://blog.csdn.net/aaa123524457/article/details/47058675 #region 业°??务?处?|理¤¨?函??数?y
///<summary> ///返¤??回?与??输??入¨?字??符¤?串??相¨¤似?的??所¨?有?D字??符¤?串?? ///</summary> ///<param name="msg"></param> ///<returns></returns> public List<string> getMatchDatas(string msg) { return this.ListItemMSG.Where ( f => ((f.Length >= msg.Length) && (((f.Substring(0, msg.Length)).ToLower()) == (msg.ToLower()))) ).ToList<string>(); }
///<summary> ///添?¨?加¨?string类¤¨¤型¨?的??数?y据Y ///</summary> ///<param name="msg">数?y据Y</param> ///<returns>成¨?功|返¤??回?1</returns> public int add(string msg) { this.ListItemMSG.Add(msg); return 1; }
///<summary> ///清?除yitems ///</summary> ///<returns></returns> public int clear() { this.ListItemMSG.Clear(); return 1; } #endregion }
#endregion
#region 重?要°a变??量?? ///<summary> ///项?目?处?|理¤¨? ///</summary> public MyItems myItems = new MyItems();
#endregion
#region 构1造¨?函??数?y ///<summary> ///无T参?构1造¨?函??数?y ///</summary> public MyComboBox() { InitializeComponent(); //this.listBox_ComboBox.ItemsSource = this.myItems.getMyItems(); }
#endregion
#region 业°??务?处?|理¤¨?函??数?y ///<summary> ///监¨¤控?键¨?盘¨?操¨?作??? ///</summary> ///<param name="sender"></param> ///<param name="e"></param> private void textBox_ComboBox_KeyUp(object sender,KeyEventArgs e) { TextBox tb = senderasTextBox;
if (tb == null) { return; }
// 键¨?盘¨?“??↑¨?”??、?é“??←?”??键¨? if ((e.Key == Key.Up) || (e.Key == Key.Left)) { if (this.listBox_ComboBox.SelectedIndex > 0) { this.listBox_ComboBox.SelectedIndex--; } } // 键¨?盘¨?“??↓y”??、?é“??→¨?”??键¨? else if ((e.Key == Key.Down) || (e.Key == Key.Right)) { if (this.listBox_ComboBox.SelectedIndex < (this.listBox_ComboBox.Items.Count - 1)) { this.listBox_ComboBox.SelectedIndex++; } } // 回?车??键¨? else if (e.Key == Key.Enter) { tb.Text = this.listBox_ComboBox.SelectedItem ==null ? tb.Text :this.listBox_ComboBox.SelectedItem.ToString(); this.listBox_ComboBox.Visibility =Visibility.Collapsed; } else { if ((tb.Text != null) && (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标?¨?位?置? tb.Select(tb.Text.Length, 1); }
///<summary> ///选?择?内¨?容¨Y ///</summary> ///<param name="sender"></param> ///<param name="e"></param> private void listBox_ComboBox_MouseDoubleClick(object sender,MouseButtonEventArgs e) { ListBox lb = senderasListBox; if (lb == null) { return; }
if (lb.SelectedItem ==null) { lb.Visibility = Visibility.Collapsed; return; }
this.textBox_ComboBox.Text = lb.SelectedItem.ToString(); lb.Visibility = Visibility.Collapsed; // 重?置?光a标?¨?位?置? 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函??数?y ///<summary> ///重?置?控?件t宽¨?度¨¨ ///</summary> ///<param name="sender"></param> ///<param name="e"></param> private void UserControl_Loaded(object sender, RoutedEventArgs e) { this.textBox_ComboBox.Width =this.ActualWidth - 1 -this.button_drop.ActualWidth; }
///<summary> ///模?ê拟a点??击??下?拉¤- ///</summary> ///<param name="sender"></param> ///<param name="e"></param> private void button_drop_Click(object sender, RoutedEventArgs e) { 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里面引用就无所谓了。
版权声明:本文为博主原创文章,未经博主允许不得转载。