Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bold and Italic text depending on data in WPF ComboBox, without XAML

Tags:

c#

combobox

wpf

I have a CustomControl that is derived from ComboBox and I would like to show certain items with Bold text, some with Italic and some normal, depending on associated data. Since there's no XAML associated with this, I am having trouble finding a way to handle this. The items are DataBound to the control via the ItemsSource property so each item type is just the Object type for my data object.

Any ideas?

like image 860
Adam Haile Avatar asked Jun 17 '11 15:06

Adam Haile


1 Answers

You can use DataTemplate for your custom ComboBox with overriding ComboBox's ItemTemplate

<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>

The CustomCombobox's ItemSource is collection of a simple object with a string property Title And an int property Style

like image 200
Navid Rahmani Avatar answered Sep 19 '22 17:09

Navid Rahmani