Bold and italic text depending on data in WPF ComboBox without XAML

I have a CustomControl that is derived from ComboBox, and I would like to show some elements with bold text, some with italics and some normal ones, depending on the associated data. Since XAML is not related to this, it's hard for me to find a way to handle this. Elements are a DataBound for the control through the ItemsSource property, so each element type is only an object type for my data object.

Any ideas?

+3
source share
2 answers

You can use DataTemplateComboBox for your custom ComboBox overrideItemTemplate

<CustomComboBox.ItemTemplate>
    <DataTemplate>
        <TextBlock x:Name="tbTitle" Text="{Binding Title}"/>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Style}" Value="0">
                <Setter TargetName="tbTitle" Property="FontWeight" Value="Bold"/>                      
            </DataTrigger>

            <DataTrigger Binding="{Binding Style}" Value="1">
                <Setter TargetName="tbTitle" Property="Foreground" Value="Red"/>
                <Setter TargetName="tbTitle" Property="FontStyle" Value="Italic"/>
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</CustomComboBox.ItemTemplate>

CustomCombobox ItemSource - Title int Style

+7

All Articles