这让我疯了!!!
我有一个用于按员工筛选查询的 ComboBox
,它运行良好,但仅显示员工的名字。我想使用 MultiValueConverter
显示员工的全名(如果我们没有两个 Mikes 和两个 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
}
}
}
The IMultiValueConverter:
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
。之后,即使您从框中选择名称,它也不会再次被调用。这些名称仍然显示为名字。
感谢您提供的任何帮助或好的教程/示例指针。我在网上找到的所有教程都是关于文本框的,而我可以一整天使用它们。