views:

238

answers:

2

I have a Silverlight test project where I'm returning a List< ProductInfo > ...

public class ProductInfo
{

    public override string ToString()
    {
        return Name + " - " + Description;
    }

    public string Name { get; set; }
    public string Description { get; set; }
}

It is successfully returning the name and description into a customized AutoCompleteBox with the following DataTemplate defined:

<DataTemplate x:Key="SearchBoxDataTemplate">
    <StackPanel Orientation="Vertical" HorizontalAlignment="Right" Margin="0, 0, 8, 0">
        <TextBlock HorizontalAlignment="Right" Foreground="Blue" 
            FontSize="18" Text="{Binding Name}" Padding="2" />
        <TextBlock HorizontalAlignment="Right" Foreground="Black" 
            FontSize="8" Text="{Binding Description}" Padding="2" />
    </StackPanel>
</DataTemplate>

However, once an item is selected, the AutoCompleteBox populates with the name of the object type (returned by a WCF service)...

IdeasAndTesting_SL_01.ServiceReference1.ProductInfo

You'll notice that I overrode the ToString() method of the ProductInfo object, so I'm not sure why this does this or how to resolve it. Ideas?

+1  A: 

Your ProductInfo and the service reference's may actually be different objects.

Consider setting the ValueMemberPath or ValueMemberBinding property on the AutoCompleteBox to a property (such as DisplayText, which returns the same Name + " - " + Description).

A value member property will always be respected over the ToString of an object.

Jeff Wilcox
+2  A: 

Make a partial class of your ProductInfo that has the same namespace as the generated service proxy class, override ToString() inside there.

markti
+1, markti's answer is where I was headed :-)
Jeff Wilcox