locked
Custom objects in ComboBox displaying class name

    Question

  • I'm loading the Items of a ComboBox with a custom winrt ref class. The problem is that when displaying the contents of the ComboBox the name of the custom class is displayed. In .Net I would simply override the ToString() function to solve this problem. From my research the ToString() function of winrt objects is not virtual or overridable. How can I do the same in winrt. I solution I thought of was to set the ComboBoxItem.Content to a descriptive string and the Tag property to my custom class object - clunky but it might work.
    Friday, August 16, 2013 8:33 PM

Answers

  • You should set the ComboBox's ItemTemplate to display the class properties you want to show. See Binding to collections. This is recommended for .Net as well, although you can get away with overriding ToString() for some uses.

    You will also want to bind the AutomationProperties.Name to something useful so your app doesn't report class names to accessibility apps.

    See the XAML accessibility sample (Windows 8) for an example.

    --Rob

    Friday, August 16, 2013 10:11 PM
    Owner

All replies

  • You should set the ComboBox's ItemTemplate to display the class properties you want to show. See Binding to collections. This is recommended for .Net as well, although you can get away with overriding ToString() for some uses.

    You will also want to bind the AutomationProperties.Name to something useful so your app doesn't report class names to accessibility apps.

    See the XAML accessibility sample (Windows 8) for an example.

    --Rob

    Friday, August 16, 2013 10:11 PM
    Owner
  • Thanks Rob. It was a bit more work doing this in C++ than C# as I had to research using the Bindable attribute, but it's working fine now.
    Saturday, August 17, 2013 10:41 PM