如何处理组合框中的重复项
我在 silverlight 中使用 telerik 组合框时遇到问题
,我已经像这样定义了它,
<telerik:RadComboBox SelectedItem="{Binding Organisation, Mode=TwoWay}" ItemsSource="{Binding Organisations}" DisplayMemberPath="Name" />
并且绑定的 SelectedItem 属性看起来像这样,
[Required(ErrorMessage = "The organisation is required.")]
public OrganisationEntity Organisation
{
get
{
return utilityOwnerOrganisation;
}
set
{
utilityOwnerOrganisation = value;
RaisePropertyChanged(this, x => x.UtilityOwnerOrganisation);
}
}
OrganizationEntity 是这样的,
public class OrganisationEntity
{
public string Name { get; set; }
public int OrganisationId { get; set; }
}
如果列表组织中有两个像这样定义的组织,就会出现问题,
new OrganisationEntity() { Name = "Wellington City Council", OrganisationId = 34 }
new OrganisationEntity() { Name = "Wellington City Council", OrganisationId = 31 }
如果我在列表中有两个具有相同名称的项目,并将 Organization 属性(来自视图模型)设置为 OrganizationId = 31 的项目,从绑定引擎调用设置器,并选择另一个项目。
原因是 DisplayMemberPath 设置为 Name。它将名称视为键并假设它在组合框中是唯一的,但事实并非如此。
如果我取出 DisplayMemberPath 它可以工作,但是组合框会显示错误的内容。
如果我取出 DisplayMemberPath 并定义 ToString 方法来返回 OrganizationEntity 中的 Name 属性,则会发生相同的情况。
I am having trouble with a telerik combobox in silverlight
I have defined it like this,
<telerik:RadComboBox SelectedItem="{Binding Organisation, Mode=TwoWay}" ItemsSource="{Binding Organisations}" DisplayMemberPath="Name" />
And the SelectedItem property bound to looks like this,
[Required(ErrorMessage = "The organisation is required.")]
public OrganisationEntity Organisation
{
get
{
return utilityOwnerOrganisation;
}
set
{
utilityOwnerOrganisation = value;
RaisePropertyChanged(this, x => x.UtilityOwnerOrganisation);
}
}
The OrganisationEntity is like this,
public class OrganisationEntity
{
public string Name { get; set; }
public int OrganisationId { get; set; }
}
The problem arises if the list Organisations have two organisations in it defined like this,
new OrganisationEntity() { Name = "Wellington City Council", OrganisationId = 34 }
new OrganisationEntity() { Name = "Wellington City Council", OrganisationId = 31 }
If I have two items with the same Name in the list and set the Organisation property (from the viewmodel) to be the item with OrganisationId = 31 the setter is called from the binding engine and the other item gets selected.
The reason is because of DisplayMemberPath being set to Name. It is treating the Name as being a key and assuming that it is unique in the combobox, but it isn't.
If I take out the DisplayMemberPath it works, but the combobox then displays the wrong thing.
If I take out the DisplayMemberPath and define a ToString method to return the Name property in the OrganisationEntity the same thing occurs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试删除 DisplayMemberPath 并使用 DataTemplate 来显示您想要的内容:
Try to remove the DisplayMemberPath and use a DataTemplate to show what you want instead: