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

Binding ItemsSource to complex business model

1 Answer 67 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Joshua
Top achievements
Rank 2
Joshua asked on 07 Oct 2014, 10:29 PM
I'm binding the ItemsSource of the AutoCompleteBox to a list of a custom object, and keep getting an error when I set the source to my list.
Here's the custom object I'm using:
class MyObject : IComparable
    {
        public string Name { get; set; }
        public string EmailAddress { get; set; }
 
        public SolidColorBrush Color { get; set; }
 
        public string FullString { get; set; }
 
        public MyObject(string name, string emailAddress, SolidColorBrush color)
        {
            Name = name;
            EmailAddress = emailAddress;
            Color = color;
            FullString = string.Format("{0} <{1}>", Name, EmailAddress);
        }
 
        public override bool Equals(object obj)
        {
            if (obj.GetType() != typeof(MyObject))
            {
                return false;
            }
            MyObject obj2 = obj as MyObject;
            if (this.FullString == obj2.FullString)
            {
                return true;
            }
            return false;
        }
 
        public int CompareTo(object obj)
        {
            if (obj.GetType() != typeof(MyObject))
            {
                return 0;
            }
            MyObject obj2 = obj as MyObject;
            if (this.FullString == obj2.FullString)
            {
                return 1;
            }
            return 0;
        }
    }


And when I set the ItemsSource, it throws an InvalidOperationException:
"An exception of type 'System.InvalidOperationException' occurred in Telerik.Universal.UI.Xaml.Input.DLL but was not handled in user codeAdditional information: Failed to compare two elements in the array."

Here's my Xaml:
<telerik:RadAutoCompleteBox x:Name="textBox" FilterMemberPath="FullString" VerticalAlignment="Top" Margin="10,20" FilterMode="Contains" DisplayMemberPath="FullString" >
            <telerik:RadAutoCompleteBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="35"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Border Margin="5" Background="{Binding Color}"/>
                        <TextBlock Text="{Binding FullString}" Grid.Column="1" VerticalAlignment="Center" Margin="5" telerik:RadAutoCompleteBox.IsTextMatchHighlightEnabled="True" />
                    </Grid>
                </DataTemplate>
            </telerik:RadAutoCompleteBox.ItemTemplate>
        </telerik:RadAutoCompleteBox>


Thanks,
Joshua

1 Answer, 1 is accepted

Sort by
0
Rosy Topchiyska
Telerik team
answered on 10 Oct 2014, 03:17 PM
Hi Joshua,

Thank you for contacting us.

The MyObject class should be public. I hope this helps.

Regards,
Rosy Topchiyska
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
Asked by
Joshua
Top achievements
Rank 2
Answers by
Rosy Topchiyska
Telerik team
Share this question
or