This is a migrated thread and some comments may be shown as answers.

[Solved] InvalidCastException in Telerik.Core.dll using RadAutoCompleteBox

2 Answers 93 Views
AutoCompleteBox for XAML
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Will
Top achievements
Rank 1
Will asked on 04 Jul 2014, 08:07 PM
Hi,

I'm using the RadAutoCompleteBox to implement a kind of "universal search" box in our application. At the moment, I have two lists;

List<ToDoTask> Tasks
List<Patient> Patients

I want both of these to be searchable, so I have made the classes implement the following common interface:

public interface ISearchable
{
    string SearchFilterString { get; }
    string SearchDisplayString { get; }
}

And the binding for the RadAutoCompleteBox is:

List<ISearchable> Suggestions

which is a cast and concatenation of the two lists. I have set FilterMemberPath and DisplayMemberPath to the appropriate member names, as below:

<telerik_in:RadAutoCompleteBox FilterMemberPath="SearchFilterString" DisplayMemberPath="SearchDisplayString" FilterMode="Contains" ItemsSource="{Binding Suggestions}" Foreground="{StaticResource ForegroundBrush}" BorderThickness="0,0,0,2" BorderBrush="{StaticResource ForegroundBrush}" HorizontalAlignment="Stretch" Grid.Row="1" Grid.ColumnSpan="2" VerticalAlignment="Top" Margin="70,25,25,0"/>

The control works fine if the source only contains one of the two lists, but if I provide the concatenated lists, I get an InvalidCastException
Unable to cast object of type 'ViewModels.DataViewModels.Patient' to type 'ViewModels.DataViewModels.ToDoTask'."

A screenshot of the call stack is attached.

I don't fully understand why it is trying to cast from one type to the other when the List is of ISearchable and in theory, the comparer shouldn't know what the types are beyond the interface provided. Am I being thick? Could anyone cast some light on this?






2 Answers, 1 is accepted

Sort by
0
Will
Top achievements
Rank 1
answered on 08 Jul 2014, 10:23 AM
For anyone else with a similar problem, my current workaround for this is to define a wrapper class like so:

public class TelerikAutoCompleteBoxHelper
{
    public ISearchable Item { get; set; }
 
    public string SearchDisplayString
    {
        get
        {
            return Item.SearchDisplayString;
        }
    }
 
    public string SearchFilterString
    {
        get
        {
            return Item.SearchFilterString;
        }
    }
}

And then assemble your list like so:

IEnumerable<TelerikAutoCompleteBoxHelper> tasks = from task in Tasks select new TelerikAutoCompleteBoxHelper() { Item = task };
IEnumerable<TelerikAutoCompleteBoxHelper> patients = from patient in Patients select new TelerikAutoCompleteBoxHelper() { Item = patient };
 
Suggestions = new List<TelerikAutoCompleteBoxHelper>();
Suggestions.AddRange(patients);
Suggestions.AddRange(tasks);
0
Accepted
Tsvyatko
Telerik team
answered on 10 Jul 2014, 07:05 AM
Hello Will,

Thank you for your feedback. We have reproduced this behavior from our side and confirmed it as bug. It will be addressed in our next official release.

I have updated your telerik points accordingly.

Regards,
Tsvyatko
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
AutoCompleteBox for XAML
Asked by
Will
Top achievements
Rank 1
Answers by
Will
Top achievements
Rank 1
Tsvyatko
Telerik team
Share this question
or