此示例看起來像 「文本搜索」
在XAML文件,你應該只添加一個屬性組合框 「TextContainSearch.Text」:
<ComboBox ItemsSource="{Binding Model.formListIntDeviceNumbers}" SelectedItem="{Binding Path=Model.selectedDeviceNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="DeviceNumber" IsEditable="True" c:TextContainSearch.Text="DeviceNumber">
我們應添加在XAML文件的標題中使用:
xmlns:c="clr-namespace:Adaptive.Controls.Extension"
而在* cs文件C#代碼:
using System;
using System.Windows;
using System.Windows.Controls;
namespace Adaptive.Controls.Extension
{
public sealed class TextContainSearch : DependencyObject {
public static void SetText(DependencyObject element, string text) {
var controlSearch = element as Control;
if (controlSearch != null)
controlSearch.KeyUp += (sender, e) =>
{
if (sender is ComboBox){
var control = sender as ComboBox;
control.IsDropDownOpen = true;
var oldText = control.Text;
foreach(var itemFromSource in control.ItemsSource){
if (itemFromSource != null)
{
Object simpleType = itemFromSource.GetType().GetProperty(text).GetValue(itemFromSource, null);
String propertOfList = simpleType as string;
if (!string.IsNullOrEmpty(propertOfList) && propertOfList.Contains(control.Text))
{
control.SelectedItem = itemFromSource;
control.Items.MoveCurrentTo(itemFromSource);
break;
}
}
}
control.Text = oldText;
TextBox txt = control.Template.FindName("PART_EditableTextBox", control) as TextBox;
if (txt != null)
{
txt.Select(txt.Text.Length, 0);
}
}
};
}
}
}
據我知道做到這一點的唯一方法是創建一個擴展組合框控件,並添加您需要的功能。 – 2013-03-15 08:08:48