views:

271

answers:

1

Hello,
What is the simplest way to customize the content display of ComboBoxItem? I would like to present a list of font names, with each item drawn in the appropriate font.
I suspect I need something like

<TextBlock FontFamily="{TemplateBinding Content}" Text="{TemplateBinding Content}" />

Where would that go?
Can I create a control template for ComboBoxItem, and use it for only selected ComboBoxes?
Thanks for any hints....

+4  A: 

Silverlight 3 does not have implicit styling, so if you create a style with a ControlTemplate to accomplish this, you would have to explicitly assign it to the ComboBox. No worries about it getting accidentally picked up.

I was able to achieve this with the following XAML inside the ComboBox, bound to an ObservableCollection<string>:

<ComboBox.ItemTemplate>
    <DataTemplate>
        <TextBlock FontFamily="{Binding}" Text="{Binding}"/>
    </DataTemplate>
</ComboBox.ItemTemplate>

Obviously, the Binding would probably need a property name if you're binding to something more complex, but this allowed me to see the various selections in their own font.

You could also refactor this either into a DataTemplate or a style if you find yourself reusing it.

Ben Von Handorf
Good answer, I was going to suggest the Item source be a collection of `FontFamily` but this works just as well. Has me wondering how the string gets converted to a `FontFamily` when assigned to the `FontFamily` property, I can't see where that is done unless the default binding handles that implicitly. Can't find where that is documented though.
AnthonyWJones
There must be an implicit Binding Converter to handle it. Given that you can specify it as a string in XAML and have it correctly look up the right font, it's probably flowing through the same mechanic.
Ben Von Handorf
Thanks, looks like just what I was looking for.
Number8