这让我发疯了!!!
我有一个ComboBox
用来过滤员工的查询,它工作正常,但只显示员工的名字。我想使用 aMultiValueConverter
来显示员工的全名(如果我们没有 2 Mikes 和 2 Daves,这将不那么紧急)
下面是我的工作代码和IMultiValueConverter
类(为简洁起见,删除了不必要的格式)。我已经尝试了所有我能想到的让 MultiConverter 工作的方法,但我没有运气。
<ComboBox ItemsSource="{Binding Path=EmployeesFilter}"
DisplayMemberPath="EmpFirstName"
SelectedValue="{Binding Path=EmployeeToShow, Mode=TwoWay}"/>
它绑定到的 ViewModel 属性:
// This collection is used to populate the Employee Filter ComboBox
private ObservableCollection<Employee> employeesFilter;
public ObservableCollection<Employee> EmployeesFilter
{
get {
return employeesFilter;
}
set {
if (employeesFilter != value)
{
employeesFilter = value;
OnPropertyChanged("EmployeesFilter");
}
}
}
// This property is TwoWay bound to the EmployeeFilters SelectedValue
private Employee employeeToShow;
public Employee EmployeeToShow
{
get {
return employeeToShow;
}
set {
if (employeeToShow != value)
{
employeeToShow = value;
OnPropertyChanged("EmployeeToShow");
QueryIssues(); // Requery with new employee filter
}
}
}
IMultiValue 转换器:
class StringsToFullNameMultiConverter : IMultiValueConverter
{
public object Convert(object[] values,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
// I added this because I kept getting DependecyProperty.UnsetValue
// Passed in as the program initializes
if (values[0] as string != null)
{
string firstName = (string)values[0];
string lastName = (string)values[1];
string fullName = firstName + " " + lastName;
return fullName;
}
return null;
}
public object[] ConvertBack(object value,
Type[] targetTypes,
object parameter,
System.Globalization.CultureInfo culture)
{
return null;
}
}
我尝试了很多不同的东西,但基本上在 ComboBox 中使用以下内容
<ComboBox.SelectedValue>
<MultiBinding Converter="{StaticResource StringsToFullNameMultiConverter}"
Mode="OneWay" >
<Binding Path="EmpFirstName" />
<Binding Path="EmpLastName"/>
</MultiBinding>
</ComboBox.SelectedValue>
就目前而言,当程序使用设置为的值初始化时调用转换器DependencyProperty.UnsetValue
。之后,即使您从框中选择了一个名称,它也不再被调用。名称仍显示为名字。
感谢您提供的任何帮助或指向您可以提供的良好教程/示例的指针。我一直在网上找到的所有文本框都是用于文本框的,我可以整天使用它们。