0

I'm working on a user control which deals with styling selections of text, I've encountered an issue where:

  • Enter the text "This is a bunch of text" into my control
  • Highlight "of text"
  • Do the thing which changes the styling of the selection
  • Press space(or enter a letter) - The styling continues when it should be unstyled

Internally, I believe this separates the FlowDocument up into 2 separate "Runs", Run 1 being "This is a bunch " and Run 2 being "of text"

However, when I press space after the styling, it just extends Run 2, changing the styling (this is a major problem)

I have tried inserting a blank Run into the document at the end of the selection using the following:

new Run(String.Empty, Selection.End);

However, this doesn;t work and the second run is still changed...

One way around this is to do the following:

new Run(" ", Selection.End);

However if I manually move the caret to the end of the style and press space it still continues the style :(

I'm a bit at the end of my tether with this, if anyone can offer any guidance it'd be greatly appreciated.

For anyone interested, here is the Source for the ICommand on the context menu (which applies the style)

    private void TagSelection(object tagType)
    {
        var type = tagType as TagType;

        var textRange = new TextRange(Selection.Start, Selection.End)
        {
            Text = Selection.Text
        };
        switch (type.Id)
        {
            case (int) TagTypeEnum.AllergenContains:
                textRange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.MediumSeaGreen));
                textRange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
                break;
            case (int)TagTypeEnum.AllergenMayContain:
                textRange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.SteelBlue));
                textRange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
                break;
            case (int)TagTypeEnum.AllergenAsOnPack:
                textRange.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.Moccasin));
                break;
        }
    }

1 Answers1

1

For anyone who stumbles across this and wondered if it ever got resolved, I came up with a solution:

I added an event to the TextChanged event:

    private void TaggableTextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        TextChanged -= TaggableTextBox_TextChanged;

        ReprocessTags();
    }

    private void ReprocessTags()
    {
        //Remove all tags, and re-process
        RemoveAllTags();
        ProcessTags();
    }

    private void RemoveAllTags()
    {
        var textRange = new TextRange(Document.ContentStart, Document.ContentEnd);
        textRange.ClearAllProperties();
    }

    private void ProcessTags()
    {
        if (Tags == null) 
            return;

        foreach (var tag in Tags.ToArray())
        {
            TagRegion(tag.Start, tag.Length, tag.Type);
        }
    }

    private void TagRegion(int index, int length, TagType type)
    {
        var start = GoToPoint(Document.ContentStart, index);
        var end = GoToPoint(start, length);

        TagSelection(type, start, end);
    }

It needs cleaning up, but basically, I clear all the formatting in the FlowDocument, and then Reprocess them, effectively creating fresh runs therefore solving the issue.

I hope this helps somebody!