使用DataTemplate时ComboBox的选定项显示错误?

发布于 2024-11-27 00:15:51 字数 2671 浏览 0 评论 0原文

我的组合框有问题。打开组合框时将显示这些项目。当我选择一个项目并且组合框关闭时,组合框显示 Model.Person 而不是 Name, Vorname

  1. 我该如何解决这个问题?
  2. 我如何实现自动建议?

我有一个带有 DataTamplate 的 ComboBox。

<ComboBox ItemTemplate="{StaticResource PersonenComboboxTemplate}"
 x:Name="Person1CheckboxName" Text="Choose Person" IsEditable="True"
 ItemsSource="{Binding Path=Personenliste}"
 SelectionChanged="Person1CheckboxName_SelectionChanged" />

<DataTemplate x:Key="PersonenComboboxTemplate">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Path=Name}"/>
        <TextBlock Text=", "/>
        <TextBlock Text="{Binding Path=Vorname}"/>
    </StackPanel>
</DataTemplate>

我使用 MVVM 模式。数据绑定是在 ViewModel 中实现的。

public ObservableCollection<Person> Personenliste
    {
        get
        {
            ObservableCollection<Person> persColl = 
                                              new ObservableCollection<Person>();
            List<Person> personen = 
                          databaseConnection.getAllPersonsRAW().ToList<Person>();
            // sort by Vorname and Nachname
            personen.Sort(new PersonComparer());
            foreach (Person p in personen)
            {
                persColl.Add(p);
            }
            return persColl;
        }
    }

Person 具有名字 (Vorname) 和姓氏 (Name)。 (由 ADO.NET 实体数据模型生成)

[EdmEntityTypeAttribute(NamespaceName="dataModel", Name="Person")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class Person : EntityObject
{
    #region Factory-Methode

    /// <summary>
    /// Erstellt ein neues Person-Objekt.
    /// </summary>
    /// <param name="personID">Anfangswert der Eigenschaft PersonID.</param>
    public static Person CreatePerson(global::System.Int64 personID)
    {
        Person person = new Person();
        person.PersonID = personID;
        return person;
    }

    #endregion
    ...
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
    [DataMemberAttribute()]
    public global::System.String Name
    {
        get
        {
            return _Name;
        }
        set
        {
            OnNameChanging(value);
            ReportPropertyChanging("Name");
            _Name = StructuralObject.SetValidValue(value, true);
            ReportPropertyChanged("Name");
            OnNameChanged();
        }
    }
    ...
}

I have a problem with the ComboBox. The items are displayed when the ComboBox is opened. When I select an item and the ComboBox is closing, the ComboBox displaying Model.Person instead of Name, Vorname.

  1. How can I solve that?
  2. How can I realize an auto suggestion?

I have a ComboBox with a DataTamplate.

<ComboBox ItemTemplate="{StaticResource PersonenComboboxTemplate}"
 x:Name="Person1CheckboxName" Text="Choose Person" IsEditable="True"
 ItemsSource="{Binding Path=Personenliste}"
 SelectionChanged="Person1CheckboxName_SelectionChanged" />

<DataTemplate x:Key="PersonenComboboxTemplate">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Path=Name}"/>
        <TextBlock Text=", "/>
        <TextBlock Text="{Binding Path=Vorname}"/>
    </StackPanel>
</DataTemplate>

I use the MVVM-Pattern. The Data Binding is implemented in the ViewModel.

public ObservableCollection<Person> Personenliste
    {
        get
        {
            ObservableCollection<Person> persColl = 
                                              new ObservableCollection<Person>();
            List<Person> personen = 
                          databaseConnection.getAllPersonsRAW().ToList<Person>();
            // sort by Vorname and Nachname
            personen.Sort(new PersonComparer());
            foreach (Person p in personen)
            {
                persColl.Add(p);
            }
            return persColl;
        }
    }

A Person has a given name (Vorname) and a surname (Name). (generated by ADO.NET Entity Data Model)

[EdmEntityTypeAttribute(NamespaceName="dataModel", Name="Person")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class Person : EntityObject
{
    #region Factory-Methode

    /// <summary>
    /// Erstellt ein neues Person-Objekt.
    /// </summary>
    /// <param name="personID">Anfangswert der Eigenschaft PersonID.</param>
    public static Person CreatePerson(global::System.Int64 personID)
    {
        Person person = new Person();
        person.PersonID = personID;
        return person;
    }

    #endregion
    ...
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
    [DataMemberAttribute()]
    public global::System.String Name
    {
        get
        {
            return _Name;
        }
        set
        {
            OnNameChanging(value);
            ReportPropertyChanging("Name");
            _Name = StructuralObject.SetValidValue(value, true);
            ReportPropertyChanged("Name");
            OnNameChanged();
        }
    }
    ...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弥枳 2024-12-04 00:15:51

重写 Person 类(或分部类)中的 ToString() 以返回 Name + " " + Vorname

public partial class Person
{
    public override ToString()
    {
        return string.Format("{0} {1}", Name, Vorname);
    }
}

编辑:

由于 ToString() 不会对更改通知做出反应,正如 HB 在评论中指出的,您可以使用第二种方法:

// In PersonPartial.cs
public partial class Person
{
    public string DisplayText
    {
        get { return string.Format("{0} {1}", Name, Vorname); }
    }

    partial void OnNameChanged()
    {
        OnPropertyChanged("DisplayText");
    }

    partial void OnVornameChanged()
    {
        OnPropertyChanged("DisplayText");
    }
}
<ComboBox ... DisplayMemberPath="DisplayText" />    

Override ToString() in Person class (or partial class) to return Name + " " + Vorname:

public partial class Person
{
    public override ToString()
    {
        return string.Format("{0} {1}", Name, Vorname);
    }
}

Edit:

Since ToString() is not something that reacts to change notifications, as H.B. pointed out in the comments, you can use this second approach:

// In PersonPartial.cs
public partial class Person
{
    public string DisplayText
    {
        get { return string.Format("{0} {1}", Name, Vorname); }
    }

    partial void OnNameChanged()
    {
        OnPropertyChanged("DisplayText");
    }

    partial void OnVornameChanged()
    {
        OnPropertyChanged("DisplayText");
    }
}
<ComboBox ... DisplayMemberPath="DisplayText" />    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文