如何处理组合框中的重复项

发布于 2024-12-12 02:06:57 字数 1317 浏览 0 评论 0原文

我在 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);
    }
}

Org​​anizationEntity 是这样的,

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 技术交流群。

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

发布评论

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

评论(1

弥繁 2024-12-19 02:06:57

尝试删除 DisplayMemberPath 并使用 DataTemplate 来显示您想要的内容:

<ComboBox ItemsSource="{Binding Organisations}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

Try to remove the DisplayMemberPath and use a DataTemplate to show what you want instead:

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