在做企业应用的时候,经常遇到使用拼音检索来快速录入的需求,因此自己动手增强了AutoCompleteBox控件。
1、增加SelectedValue属性,用于实现key/value;
2、增加SearchUsingPinyin属性,用于使用拼音快速检索;
3、增加ShowDropDownToggle属性,用于切换下拉按钮,以实现ComboBox的功能;
补充:
在Silverlight中的AutoComplete与asp.net中的AutoComplete中的过滤机理是不一样的,在asp.net中虽然
每次Fileter都要调用WebServices,但是可以适应数据量较大的状况。而在Silverlight中却是一次读入所有的Items的,
我测试记录达到5000以上时,装载速度就比较慢了。不知有什么好方法没有?欢迎告知。
演示地址发到这:http://goo.gl/M4Z2
MainPage.xaml
<UserControl x:Class="AutoCompleteTest2.MainPage"
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:local="clr-namespace:AutoCompleteTest2"
mc:Ignorable="d"
d:DesignHeight="372" d:DesignWidth="575"
xmlns:my="clr-namespace:Aizhe.Controls;assembly=Aizhe.Controls"
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<UserControl.Resources>
<local:SimpleEmployeeViewModel x:Key="SimpleEmployeeViewModel"/>
<DataTemplate x:Key="AirportDataTemplate">
<Grid Width="160" ShowGridLines="False">
<StackPanel HorizontalAlignment="Left" Margin="0,0,2,0" Orientation="Horizontal">
<TextBlock Foreground="Green" Text="{Binding Name}" Padding="0,0,2,0"/>
<TextBlock Foreground="Green" Text="("/>
<TextBlock Foreground="Green" Text="{Binding PersonId}"/>
<TextBlock Foreground="Green" Text=")"/>
</StackPanel>
<StackPanel HorizontalAlignment="Right" Margin="0, 0, 0, 0" Orientation="Horizontal">
<TextBlock Foreground="Gray" Text="-" Padding="2,2"/>
<TextBlock HorizontalAlignment="Right" Foreground="Gray" Text="{Binding OrgName}" />
</StackPanel>
</Grid>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<my:AizheAutoCompleteBox Height="28" HorizontalAlignment="Left" Margin="18,132,0,0" Name="aizheAutoCompleteBox1"
VerticalAlignment="Top" Width="160"
ItemsSource="{Binding Source={StaticResource SimpleEmployeeViewModel},
Path=GetAllSimpleEmployees}"
ItemTemplate="{StaticResource AirportDataTemplate}"
MinimumPrefixLength="{Binding ElementName=numericUpDown1, Path=Value, Mode=OneWay}"
ShowDropDownToggle="{Binding ElementName=ShowDropDownToggleCheckBox, Path=IsChecked, Mode=OneWay}"
SelectedValuePath="PersonId"
SearchUsingPinyin="{Binding ElementName=checkBox1, Path=IsChecked}" />
<CheckBox Content="ShowDropDownToggle" Height="16" HorizontalAlignment="Left" Margin="14,25,0,0"
Name="ShowDropDownToggleCheckBox" VerticalAlignment="Top" Width="161" DataContext="{Binding}" IsChecked="False" />
<toolkit:NumericUpDown Height="22" HorizontalAlignment="Left" Margin="147,92,0,0" Name="numericUpDown1" VerticalAlignment="Top" />
<sdk:Label Height="28" Margin="18,92,427,0" Name="label1" VerticalAlignment="Top" Content="MinimumPrefixLength:" />
<sdk:Label Height="28" HorizontalAlignment="Left" Margin="257,136,0,0" Name="label2"
VerticalAlignment="Top" Width="90" Content="SelectedValue:" />
<sdk:Label Height="28" HorizontalAlignment="Left" Margin="356,136,0,0" Name="label3"
VerticalAlignment="Top" Width="77" Foreground="DarkGreen"
Content="{Binding Path=SelectedValue, ElementName=aizheAutoCompleteBox1}" />
<sdk:Label Height="28" HorizontalAlignment="Left" Margin="257,223,0,0" Name="label4"
VerticalAlignment="Top" Width="147" Content="SelectedItem's OrgName:" />
<sdk:Label Height="28" HorizontalAlignment="Left" Margin="410,223,0,0" Name="label5"
VerticalAlignment="Top" Width="120" Foreground="#FFAE0000"
Content="{Binding Path=SelectedItem.OrgName, ElementName=aizheAutoCompleteBox1}" />
<sdk:Label Height="28" HorizontalAlignment="Left" Margin="258,265,0,0" Name="label6"
VerticalAlignment="Top" Width="146" Content="SelectedItem's OrgId:" />
<sdk:Label Height="28" HorizontalAlignment="Left" Margin="410,265,0,0" Name="label7"
VerticalAlignment="Top" Width="120" Foreground="#FFB50000"
Content="{Binding Path=SelectedItem.OrgId, ElementName=aizheAutoCompleteBox1}" />
<CheckBox Content="SearchUsingPinyin" Height="16" HorizontalAlignment="Left" Margin="16,55,0,0" Name="checkBox1"
VerticalAlignment="Top" IsChecked="True" />
<sdk:Label Height="28" HorizontalAlignment="Left" Margin="255,178,0,0" Name="label8"
VerticalAlignment="Top" Width="126" Content="SelectedItem's Name:" />
<sdk:Label Height="28" HorizontalAlignment="Left" Margin="397,174,0,0" Name="label9"
VerticalAlignment="Top" Width="120"
Content="{Binding Path=SelectedItem.Name, ElementName=aizheAutoCompleteBox1}" Foreground="#FF990000" />
<sdk:Label Height="28" HorizontalAlignment="Left" Margin="19,171,0,0" Name="label10"
VerticalAlignment="Top" Width="120" Content="example: lhj, 李h,w大" />
</Grid>
</UserControl>
AizheAutoCompleteBox.cs
/*
* Author: lihongjun 2010/9/28 7:08
* 为了给AutoCompleteBox实现key/value功能,继承它的基础上扩展了两个依赖属性。
* 一个是SelectedValuePath 为了指定值(Value)路径
* 一个是SelectedValue 用于设置或获取值(Value)数据
* reversion: 2010/09/29 1:51
* 1.修正重复记录只选第一条的bug
* 2.增加SpellCodePath属性
* reversion: 2010/10/3 23:56
* 1.增加SearchUsingPinyin属性
* 2.增加Pinyin类,用以实现拼音与汉字混和Search
* todo:
*
*/
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.Reflection;
using System.Windows.Controls.Primitives;
using Aizhe.Utilities;
namespace Aizhe.Controls
{
public class AizheAutoCompleteBox : AutoCompleteBox
{
private bool IsBatchUpdating = false;
public AizheAutoCompleteBox()
: base()
{
this.DefaultStyleKey = typeof(AizheAutoCompleteBox);
SetCustomFilter();
}
#region SelectedValuePath
public static readonly DependencyProperty SelectedValuePathProperty =
DependencyProperty.Register("SelectedValuePath",
typeof(string),
typeof(AizheAutoCompleteBox), null);
/// <summary>
/// 获取或设置选中值的Path,用于实现Key/Value Pair
/// </summary>
public string SelectedValuePath
{
get { return GetValue(SelectedValuePathProperty) as string; }
set { SetValue(SelectedValuePathProperty, value); }
}
#endregion
#region SelectedValue
public static readonly DependencyProperty SelectedValueProperty =
DependencyProperty.Register("SelectedValue", typeof(object),
typeof(AizheAutoCompleteBox),
new PropertyMetadata(new PropertyChangedCallback(OnSelectedValueChanged))
);
/// <summary>
/// gets or sets SelectedValue,used to implement key value piar.
/// </summary>
public object SelectedValue
{
get { return GetValue(SelectedValueProperty); }
set { SetValue(SelectedValueProperty, value); }
}
private static void OnSelectedValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((AizheAutoCompleteBox)d).OnSelectedValueChanged(e);
}
protected virtual void OnSelectedValueChanged(DependencyPropertyChangedEventArgs e)
{
if (!IsBatchUpdating)
SetSelectedItemUsingSelectedValue();
}
/// <summary>
/// sets SelectedItem using SelectedValue.
/// </summary>
private void SetSelectedItemUsingSelectedValue()
{
if (this.SelectedValue == null || this.ItemsSource ==null)
this.SelectedItem = null;
else
{
string selectedValuePath = this.SelectedValuePath;
object selectValue = GetValue(SelectedValueProperty);
if ((!string.IsNullOrEmpty(selectedValuePath)) && selectValue != null)
{
foreach (object item in this.ItemsSource)
{
PropertyInfo propertyInfo = item.GetType().GetProperty(selectedValuePath);
object propertyValue = propertyInfo.GetValue(item, null);
if (propertyValue != null && propertyValue.Equals(selectValue))
{
this.SelectedItem = item;
break;
}
}
}
}
}
#endregion SelectedValue
#region SpellCodePath
public static readonly DependencyProperty SpellCodePathProperty=
DependencyProperty.Register("SpellCodePath",
typeof(string),
typeof(AizheAutoCompleteBox),null
/*new PropertyMetadata(new PropertyChangedCallback(OnSpellCodePathChanged))*/
);
/// <summary>
/// gets or sets SpellCode Path.
/// </summary>
public string SpellCodePath
{
get { return GetValue(SpellCodePathProperty) as string; }
set
{
SetValue(SpellCodePathProperty, value);
//SetSpellCodeFilter();
}
}
//private static void OnSpellCodePathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
//{
// ((AizheAutoCompleteBox)d).OnSelectedValueChanged(e);
//}
//protected virtual void OnSpellCodePathChanged(DependencyPropertyChangedEventArgs e)
//{
// SetSpellCodeFilter();
//}
#endregion
#region ShowDropDownToggle
public static readonly DependencyProperty ShowDropDownToggleProperty =
DependencyProperty.Register("ShowDropDownToggle", typeof(Boolean), typeof(AizheAutoCompleteBox),
new PropertyMetadata(new PropertyChangedCallback(OnShowDropDownToggleChanged)));
public bool ShowDropDownToggle
{
get { return (bool)GetValue(ShowDropDownToggleProperty); }
set
{
SetValue(ShowDropDownToggleProperty, value);
}
}
private static void OnShowDropDownToggleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((AizheAutoCompleteBox)d).OnShowDropDownToggleChanged(e);
}
protected virtual void OnShowDropDownToggleChanged(DependencyPropertyChangedEventArgs e)
{
SetDropDownToggleDisplayState(ShowDropDownToggle);
}
protected virtual void SetDropDownToggleDisplayState(bool isShowing)
{
ToggleButton dropdownButton = (ToggleButton)GetTemplateChild("DropDownToggle");
if (dropdownButton != null)
{
if (isShowing)
dropdownButton.Visibility = System.Windows.Visibility.Visible;
else
dropdownButton.Visibility = System.Windows.Visibility.Collapsed;
}
}
#endregion
#region SearchUsingPinyin
public static readonly DependencyProperty SearchUsingPinyinProperty =
DependencyProperty.Register("SearchUsingPinyin", typeof(bool),
typeof(AizheAutoCompleteBox), null);
public bool SearchUsingPinyin
{
get { return (bool)GetValue(SearchUsingPinyinProperty); }
set { SetValue(SearchUsingPinyinProperty, value); }
}
#endregion

浙公网安备 33010602011771号