使用DataTemplate时ComboBox的选定项显示错误?
我的组合框有问题。打开组合框时将显示这些项目。当我选择一个项目并且组合框关闭时,组合框显示 Model.Person
而不是 Name, Vorname
。
- 我该如何解决这个问题?
- 我如何实现自动建议?
我有一个带有 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
.
- How can I solve that?
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
重写 Person 类(或分部类)中的 ToString() 以返回
Name + " " + Vorname
:编辑:
由于 ToString() 不会对更改通知做出反应,正如 HB 在评论中指出的,您可以使用第二种方法:
Override ToString() in Person class (or partial class) to return
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: