【问题标题】:How to compare ItemElements of a Radcombobox with an expected string?如何将 Radcombobox 的 ItemElements 与预期的字符串进行比较?
【发布时间】:2016-08-26 06:25:30
【问题描述】:

我需要比较 RadcomboboxItemElements 是否与我预期的字符串匹配。这是我想要做的:

foreach (IRadComboBoxItem item in comboBox.ItemElements)
{
    var itemExists = comboBox.ItemElements.FirstOrDefault(items => item.Text.Contains(expectedString));
    if (itemExists == null) continue;
    itemExists.Select();
    return true;
}

但是 comboBox.Text.Contains(expectedString) 不受支持,因为我将 IRadComboBoxItem 与字符串进行比较。您能否建议如何实现这一目标?

【问题讨论】:

    标签: c# linq radcombobox telerik-testing-tools


    【解决方案1】:

    使用Any的linq方法:

    return comboBox.ItemElements.Any(item => item.Text.Contains(expectedString));
    

    在上面的代码中,你混合了不同 linq 方法的使用

    1. FirstOrDefault - 它返回集合中与谓词匹配的第一项,否则返回default(T)
    2. 如果不是null,则执行Select,但将其分配给任何地方。
    3. 您在foreach 循环中有此代码 - 但不要在任何地方使用item。您不需要循环,因为您正在尝试使用 linq 方法(在幕后使用循环本身)

    下面的评论你想要的是:

    var wantedItem = comboBox.ItemElements.FirstOrDefault(item => item.Text.Contains(expectedString));
    if(wantedItem != null)
    {
        //What you want to do with item
    }
    

    我本人没有与 RadComboBox 合作,但可能与 this site 合作:

    RadComboBoxItem item = comboBox.FindItemByText(expectedString);
    

    我假设如果没有找到它会返回 null

    【讨论】:

    • 感谢您的回复。我更正了我的代码。但是你的建议并没有解决我的问题。我有一个数据绑定的组合框。所以 Text 属性不可用。尽管 comboBox.ItemElements 返回元素,但 item.Text 始终保持 null 。所以我无法比较字符串。此外,我使用 Select 从组合框中选择与预期字符串匹配的元素
    猜你喜欢
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 2022-08-14
    • 1970-01-01
    相关资源
    最近更新 更多