【发布时间】:2014-04-24 00:25:07
【问题描述】:
我在 wpf 和 xaml 中的 ComboBox 有问题。我的组合框的 ItemsSource 是一个自定义类 (Contacts.cs)。如果我输入一个字符串(长度 > 2),它 DropDownList 应该只包含包含我输入的子字符串的项目。这很好用。但知道我还有其他问题。我用一个例子来解释:
- 选择一项 (Testi2)
- 删除最后一个字符 (Testi)
- 下拉菜单打开 itemssource 显示所有包含“Testi”的项目
- 文本框显示 Testi2
- 预期结果:文本框显示 Testi
我认为 SelectedItem 有问题,但我不知道如何解决。
组合框行为的图像 http://postimg.org/image/dl2s4iggn/
Contacts.cs
namespace Test.Model
{
class Contacts
{
private string name;
private string age;
public Contacts(string name, string age)
{
this.name = name;
this.age = age;
}
public Contacts()
{
}
public string Name
{
get { return name; }
set { name = value; }
}
public string Age
{
get { return age; }
set { age = value; }
}
}
}
MainWindow.xaml
<custom:CustomComboBox x:Name="CustomCombo"
Grid.Row="1"
Grid.Column="1"
ItemsSource="{Binding CustomContacts}"
SelectedItem="{Binding CustomSelectedContact}"
DisplayMemberPath="Name"
Margin="5"
IsEditable="True"
IsTextSearchEnabled="False"
KeyUp="CustomComboBox_KeyUp"/>
<TextBlock Grid.Row="1"
Grid.Column="2"
Margin="10,5,5,0”
VerticalAlignment="Center">
<Run Text="{Binding CustomSelectedContact.Name}"/>
<Run Text=" - "/>
<Run Text="{Binding CustomSelectedContact.Age}"/>
</TextBlock>
MainWindow.caml.cs
namespace Test.View
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var viewModel = new MainWindowViewModel();
DataContext = viewModel;
}
private void CustomComboBox_KeyUp(object sender, KeyEventArgs e)
{
if (CustomCombo.Text.Length < 3)
return;
var viewModel = (MainWindowViewModel)DataContext;
viewModel.CustomSearchParameter = CustomCombo.Text;
CustomCombo.IsDropDownOpen = true;
}
}
}
MainWindowViewModel.cs
namespace Test.ViewModel
{
class MainWindowViewModel : INotifyPropertyChanged
{
private ObservableCollection<Contacts> customcontacts;
private Contacts customselectedContact;
private string customsearchParameter;
public MainWindowViewModel()
{
customcontacts = new ObservableCollection<Contacts>();
customcontacts.Add(new Contacts("Test", "asdf"));
customcontacts.Add(new Contacts("Testi1", "iasdf"));
customcontacts.Add(new Contacts("Testi2", "ijkl"));
customcontacts.Add(new Contacts("Testo", "o"));
customcontacts.Add(new Contacts("Testo2", "oasdf"));
customcontacts.Add(new Contacts("Other", "otherasdf"));
customselectedContact = new Contacts();
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
public ObservableCollection<Contacts> CustomContacts
{
get
{
if (!String.IsNullOrEmpty(CustomSearchParameter))
{
ObservableCollection<Contacts> temp = new ObservableCollection<Contacts>();
foreach (Contacts contact in customcontacts)
{
if (contact.Name.Contains(CustomSearchParameter))
{
temp.Add(contact);
}
}
return temp;
}
return customcontacts;
}
set { customcontacts = value; }
}
public Contacts CustomSelectedContact
{
get { return customselectedContact; }
set
{
customselectedContact.Name = value.Name;
customselectedContact.Age = value.Age;
OnPropertyChanged("CustomSelectedContact");
}
}
public string CustomSearchParameter
{
get { return customsearchParameter; }
set
{
customsearchParameter = value;
OnPropertyChanged("CustomContacts");
}
}
}
}
CustomComboBox.cs
namespace Test.CustomWindowsControls
{
class CustomComboBox : ComboBox
{
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.DropDownOpened += comboBox_DropDownOpened;
}
void comboBox_DropDownOpened(object sender, EventArgs e)
{
var element = GetTemplateChild("PART_EditableTextBox");
if (element != null)
{
var txt = (TextBox)element;
if (base.IsDropDownOpen && txt.SelectionLength > 0)
{
txt.CaretIndex = base.Text.Length;
}
}
}
}
}
【问题讨论】:
标签: c# wpf xaml data-binding combobox