开发者

Selected Item of ComboBox is displayed wrong by using of DataTemplate?

开发者 https://www.devze.com 2023-03-24 08:10 出处:网络
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, Vornam

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();
      开发者_StackOverflow中文版  }
    }
    ...
}


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" />    
0

精彩评论

暂无评论...
验证码 换一张
取 消