6

这让我发疯了!!!

我有一个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
        }
    }
}

IMul​​tiValue 转换器:

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。之后,即使您从框中选择了一个名称,它也不再被调用。名称仍显示为名字。

感谢您提供的任何帮助或指向您可以提供的良好教程/示例的指针。我一直在网上找到的所有文本框都是用于文本框的,我可以整天使用它们。

4

3 回答 3

24

你很近!你想做的是ComboBox.ItemTemplate,不是SelectedValue。为一些 XAML 地狱做准备。

<ComboBox.ItemTemplate>
    <DataTemplate>
        <TextBlock>
            <TextBlock.Text>
                <MultiBinding Converter="{StaticResource StringsToFillNameMultiConverter}">
                    <Binding Path="EmpFirstName" />
                    <Binding Path="EmpLastName" />
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </DataTemplate>
</ComboBox.ItemTemplate>

另外,如果我没记错的话,如果您只是格式化字符串,则不需要创建自己的转换器。我认为您可以执行以下操作(如果我错了,请有人纠正我。)

<!-- "Last, First" -->
<MultiBinding StringFormat="{}{1}, {0}">
    <Binding Path="EmpFirstName" />
    <Binding Path="EmpLastName" />
</MultiBinding>
于 2010-02-04T06:39:46.323 回答
0

您最好为项目使用数据模板,这样您就可以完全控制每个人在下拉列表中的显示方式。

如果您不需要控制不同字段的格式,则类型转换是可以的。

于 2010-02-04T09:01:55.203 回答
0

我最终通过在我的类中添加一个只读属性并在组合框中使用 Displaymemberpath

public class MyEmployee
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string DisplayName {
    get { return FirstName + " " + LastName; }
        }
}

像这样的东西对你的情况有用吗......?BR,丹尼尔

于 2016-02-10T16:42:25.947 回答