Binding ItemsSource to complex business model

2 posts, 0 answers
  1. Joshua
    Joshua avatar
    2 posts
    Member since:
    Feb 2013

    Posted 07 Oct 2014 Link to this post

    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

  2. Rosy Topchiyska
    Admin
    Rosy Topchiyska avatar
    551 posts

    Posted 10 Oct 2014 Link to this post

    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.

     
Back to Top