如何在WPF ComboBox中使用MultiBinding

7

这让我疯了!!!

我有一个用于按员工筛选查询的 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。之后,即使您从框中选择名称,它也不会再次被调用。这些名称仍然显示为名字。

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

3个回答

25

你已经接近成功了!不过你要做的是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>

1
奇怪的是,这个方法可以工作,但是xaml中会出现错误提示,提示文本为“属性'Text'不支持'MultiBinding'类型的值。”我相信这只是一个容易解决的问题,但是由于我的主要问题已经解决,所以我将把它留到明天再处理!非常感谢你的帮助,我可不想因为未解决的谜题而失眠。 - Mike B
顺便说一句,你的代码在没有转换器的情况下进行格式化的效果非常完美! - Mike B
这是一个奇怪的错误。不过听起来可能只是一个无害的 Cider bug。 - Josh
在另一份报告中,我从微软找到了有关此问题的参考信息:“感谢您的反馈。我们已经能够重现您所看到的问题。我们正在将此问题路由到 Visual Studio 产品团队内的适当组进行分类和解决。这些专业的专家将跟进您的问题。” - Mike B

0
我最终在我的类中添加了一个只读属性,并在组合框中使用Displaymemberpath。
public class MyEmployee
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string DisplayName {
    get { return FirstName + " " + LastName; }
        }
}

你的情况可能适用这样的方案吗…? 敬礼, 丹尼尔


老实说,我不知道,原问题是在2010年的,现在已经不在我的视线范围内了 :) - Mike B
我看到了...只是想发表我的意见,以防有人需要 :) - dba

0

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

类型转换是可以的,前提是你不需要控制不同字段的格式。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接