繁体   English   中英

如何在WPF ComboBox中使用MultiBinding

[英]How to use MultiBinding in a WPF ComboBox

这让我坚持不懈!

我有一个ComboBox用于过滤员工的查询,工作正常,但只显示员工的名字。 我想使用MultiValueConverter来显示员工的全名(如果我们没有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​​tiValueConverter:

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的值初始化时,将调用转换器。 之后,即使从盒子中选择一个名字,它也永远不会被再次调用。 名称仍显示为名字。

感谢您提供的任何帮助或指向优秀教程/示例的指示。 我在网上发现的所有内容都是文本框,我可以整天使用它们。

你很亲密! 你想要做的是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>

您可能更善于为项目使用数据模板,因此您可以完全控制每个人在下拉列表中的显示方式。

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

我最终将addig一个Readonly属性添加到我的类中,并在Combobox中使用Displaymemberpath

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

这样的事可以适合你的情况......? BR,丹尼尔

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM