简体   繁体   English

具有动态ItemSource问题的Telerik RadAutoCompleteBox

[英]Telerik RadAutoCompleteBox with dynamic ItemSource problem

I have an RadAutoCompleteBox with a dynamic ItemSource that works some places and not others. 我有一个带有动态ItemSource的RadAutoCompleteBox,它可以在某些地方工作,而不是其他地方。 In the setter for the SearchText I run a search and populate the ItemSource. 在SearchText的setter中,我运行搜索并填充ItemSource。 In the box I'm working on now, the ItemSource's ObservableCollection is giving me "Exception thrown: 'System.ArgumentException' in System.Core.dll" when i try and add item to it. 在我正在处理的框中,当我尝试向其添加项目时,ItemSource的ObservableCollection给我“在System.Core.dll中抛出异常:'System.ArgumentException'”。 This method works many places elsewhere, and If i manually set the SearchText elsewhere the offending code runs without error. 此方法适用于其他地方的许多地方,如果我在其他地方手动设置SearchText,则违规代码运行时没有错误。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Here's the XAML: 这是XAML:

<telerik:RadAutoCompleteBox 
    Grid.Column="2" Grid.Row="3"
    x:Name="AutoCompleteBoxMakeModel"
    ItemsSource="{Binding MakeModelCollection}"
    SearchText="{Binding MakeModelTerm, Mode=TwoWay}"
    SelectedItem="{Binding SelectedMakeModel, Mode=TwoWay}"
    SelectionMode="Single"
    HorizontalAlignment="Left" 
    TextSearchPath="Display"
    TextSearchMode="Contains"
    AutoCompleteMode="Suggest"
    IsDropDownOpen="{Binding IsMakeModelDropDownOpen, Mode=TwoWay}"
    DropDownItemTemplate="{StaticResource MakeModelSearchTemplate}"
    DropDownWidth="300"
    VerticalAlignment="Top" Width="275"  Height="25" Margin="0,3,0,0" >
    <telerik:StyleManager.Theme>
        <telerik:VisualStudio2013Theme/>
    </telerik:StyleManager.Theme>
</telerik:RadAutoCompleteBox>

And here's the offending code: 这是违规代码:

private string makeModelTerm;
public string MakeModelTerm
{
    get { return makeModelTerm; }
    set
    {
        if (makeModelTerm == value)
        {
            return;
        }
        makeModelTerm = value;
        RaisePropertyChanged("MakeModelTerm");
        if (value.Length > 2)
        {
            SearchMakeModel(value);
        }
    }
}

private void SearchMakeModel(string value)
{
    LookUpRepository er = new LookUpRepository();
    var list = er.SearchMakeModel(value);
    MakeModelCollection.Clear();
    list.ForEach(MakeModelCollection.Add); // This triggers the System.ArgumentException on each value in list
    this.IsMakeModelDropDownOpen = true;
}

You should pass delegate to list.ForEach as below. 您应该将委托传递给list.ForEach,如下所示。

list.ForEach(item => MakeModelCollection.Add(item));

For your reference, please look at microsoft implementaion here for the same : 供大家参考,请看这里的microsoft实现:

https://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs,0e5a9cf0a310b9e5 https://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs,0e5a9cf0a310b9e5

The TextSearchPath didn't match the model. TextSearchPath与模型不匹配。

Typos sukc 错别字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM