其他分享
首页 > 其他分享> > 带有动态ItemSource问题的Telerik RadAutoCompleteBox

带有动态ItemSource问题的Telerik RadAutoCompleteBox

作者:互联网

我有一个带有动态ItemSource的RadAutoCompleteBox,可以在某些地方工作,而在其他地方工作.在SearchText的设置器中,我运行搜索并填充ItemSource.在我现在正在处理的框中,当我尝试向其中添加项目时,ItemSource的ObservableCollection给我“抛出异常:System.Core.dll中为’System.ArgumentException’”.此方法在其他地方的很多地方都可以使用,如果我在其他地方手动设置了SearchText,则有问题的代码将正常运行.任何帮助将不胜感激.

这是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>

这是令人讨厌的代码:

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;
}

解决方法:

您应该将委托传递给list.ForEach,如下所示.

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

供您参考,请在此处查看microsoft的实现:

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

标签:telerik,c
来源: https://codeday.me/bug/20191211/2106229.html