UWP AutoGenerateProperty 在 DisplayMemberPathCollection 中显示两个集合

UWP AutoGenerateProperty show two collections in DisplayMemberPathCollection

我正在尝试将组合框或目录从显示一组集合中的 属性 "Name" 修改为显示其中两个,例如 "Name" 和 "Age" 例如。

我已经尝试将它添加为第二个参数 [DisplayMemberPathCollection("Name","SecondString")] 并修改属性,使其接受两个参数。

//The autogenerated property in the model:

[AutoGenerateProperty]
[Display("User")]
[PropertyOrder(1)]
[DisplayMemberPathCollection("Name")]
[SelectedItemCollection("SelectedUser")]

//I changed it to this:
[AutoGenerateProperty]
[Display("User")]
[PropertyOrder(1)]
[DisplayMemberPathCollection("Name","Age")]
[SelectedItemCollection("SelectedUser")]

//The attribute modification I made to get two parameters:

public DisplayMemberPathCollectionAttribute(string first = "", string second = "")
{
 DisplayMemberPath = first + second;
}

我想在组合中显示这两个字段,但似乎没有任何效果,而且我还没有发现任何有用的东西

你所做的是不可能的。 DisplayMemberPath 不支持串联的字段名称。正确的方法是创建一个新字段来连接两个字段,并使 'DisplayMemberPathCollection' 引用新字段。

例如,您可以在模型中定义 'FullName' class:

public class User:PropertyChangeBase
{
    private string name;

    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            NotifyPropertyChanged();
        }
    }


    private string lastName;

    public string LastName
    {
        get { return lastName; }
        set
        {
            lastName = value;
            NotifyPropertyChanged();
        }
    }

    private string fullName;
    public string FullName
    {
        get { return Name + " " + LastName; }
    }

    private int age;

    public int Age
    {
        get { return age; }
        set
        {
            age = value;
            NotifyPropertyChanged();
        }
    }

}

'DisplayMemberPathCollection' 看起来像这样:[DisplayMemberPathCollection("FullName")],然后 comboBoxItem 将显示全名。