У меня проблема с ComboBox. Элементы отображаются при открытии ComboBox. Когда я выбираю элемент и ComboBox закрывается, ComboBox отображает Model.Person
вместо Name, Vorname
.
- Как я могу решить это?
- Как я могу реализовать автоматическое предложение?
У меня есть ComboBox с 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>
Я использую MVVM-Pattern. Привязка данных реализована во 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
имеет заданное имя (Vorname
) и фамилию (Name
). (генерируется 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();
}
}
...
}