我正在准备一个示例代码,其中当用户在 TextBox 中输入文本时会自动选择 ListBox 中的文本。到目前为止我已经达到了结果,但是没有不区分大小写的匹配。这是代码。
XAML
<StackPanel>
<TextBox Name="txt" />
<ListBox ItemsSource="{Binding Employees}" DisplayMemberPath="Name" SelectedValuePath="Name"
Height="100" SelectedValue="{Binding Text, ElementName=txt}" SelectedItem="{Binding SelectedEmployee}"/>
<Button Content="OK" Command="{Binding SaveCommand}" />
</StackPanel>
我将此 XAML 与以下 ViewModel 绑定。
视图模型
public class CheckViewModel : ViewModel.ViewModelBase
{
IList<Employee> employees;
Employee _selectedEmployee;
public CheckViewModel()
{
employees = new List<Employee>() {
new Employee(1, "Morgan"),
new Employee(2, "Ashwin"),
new Employee(3, "Shekhar"),
new Employee(5, "Jack"),
new Employee(5, "Jill")
};
}
public IList<Employee> Employees
{
get
{ return employees; }
}
public Employee SelectedEmployee
{
get
{ return _selectedEmployee; }
set
{
if (_selectedEmployee != value)
{
_selectedEmployee = value;
this.OnPropertyChanged("SelectedEmployee");
}
}
}
ICommand _saveCommand;
public ICommand SaveCommand
{
get
{
if (_saveCommand == null)
{
_saveCommand = new ViewModel.RelayCommand((p) =>
{
if(this._selectedEmployee != null)
MessageBox.Show(this._selectedEmployee.Name);
else
MessageBox.Show("None Selected");
},
(p) =>
{
return this._selectedEmployee != null;
});
}
return _saveCommand;
}
}
}
public class Employee
{
public Employee(int id, string name)
{
this.Name = name;
this.Id = id;
}
public string Name { get; set; }
public int Id { get; set; }
}
因此,当我在 TextBox 中键入“Jack”时,选择就正确发生了。但是当我改变大小写时 - “jack” - 选择不会发生并且 SelectedEmployee 变为空。当然,比较是区分大小写的,但是我怎样才能改变这种行为呢?
您能否指导我如何使比较不区分大小写?