Tekito
Tekito

Reputation: 860

editable rich text for WPF combobox

(using VS 2010)

I have a Combobox with IsEditable set to True (ie, the user can select items from the dropdown, but also edit the text of the selected item). Additionally, I want certain keywords to be automatically bolded and possibly colored. In other words, the text will not have uniform styling. This is most important for the selected item, but it would be nice if the dropdown items also followed the same formatting convention.

RichTextBox seems to be the most recommended choice for implementing multiple text formatting, but I can't figure out how to properly implement it as my ComboBox's ItemTemplate. This is what I have so far:

<ComboBox  HorizontalAlignment="Stretch" 
            SelectedItem="{Binding SearchText}"
            ItemsSource="{Binding FileHistoryItems}">

    <ComboBox.ItemTemplate>
        <DataTemplate>
                <RichTextBox Width="300" >
                    <RichTextBox.Document>
                    <FlowDocument >
                            <Paragraph>
                                <TextBlock Text="{Binding}"></TextBlock>
                            </Paragraph>
                        </FlowDocument>
                    </RichTextBox.Document>
                </RichTextBox>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

But this method is extremely clunky, with all kinds of selection/focus/editing issues. And I haven't even gotten to the code for formatting keywords yet.

Can anyone show me the proper way to use a RichTextBow for an editable combobox? Or any way to implement a combobox where the item text can have multiple formats?

Upvotes: 1

Views: 1313

Answers (1)

feO2x
feO2x

Reputation: 5728

Unfortunately, you cannot "restyle" the WPF ComboBox to use a RichTextBox by changing the control template - this is because the ComboBox expects an instance of a TextBox via the TemplatePartAttribute (you can have a look at the default style and control template of the combo box here: http://msdn.microsoft.com/en-us/library/ms752094(v=vs.100).aspx). RichTextBox does not derive from TextBox so you have no possibility to exchange it.

The next problem is that you cannot change the formatting of only some words within a regular TextBox - you can only change the formatting of the whole text but this is not the scenario you want. I just reflectored into the .NET code for ComboBox and TextBox and I would say there is no way to achieve that (as the important parts like the TextContainter class are internal).

So what are your options? You could implement your own ComboBox although this would take a lot of time and you should have a really thorough understanding of WPF - I would not advise this route.

Maybe the easiest way is to create a new control with the following control template structure:

<ControlTemplate TargetType="{x:Type l:MyComboBox}">
    <Grid>
        <ComboBox IsEditable="True">
            <ComboBox.Template>
                <ControlTemplate>
                    <!-- Default Template for ComboBox but without the TextBox and corresponding visual state changes -->
                </ControlTemplate>
            </ComboBox.Template>
        </ComboBox>
        <RichTextBox x:Name="TextHost" /> <!-- Make sure that this text box is properly aligned within ComboBox -->
    </Grid>
</ControlTemplate>

Your new control could derive from combo box and handle everything that is necessary to display the text of the current item as well as writing back the text when it is changed by the user - but this is also a lot of work and shouldn't be underestimated.

By this you would encapsulate the WPF combo box in a way. This solution might still take some days to implement but it should be faster than to build a combo box from start.

If you have any more questions, please feel free to ask.

Upvotes: 1

Related Questions