RichTextBlock keeps inserting spaces between records in UWP

I have this very simple RichtTextBlock:

    <RichTextBlock VerticalAlignment="Center">
        <Paragraph>
            <Run Text="Hello" />
            <Run Text="world" />
        </Paragraph>
    </RichTextBlock>

At design time, Runs are displayed without spaces between them ( what I need! ), But at run time, both Runs are separated by a space.

To illustrate the problem, take the following snapshots:

In the Visual Studio designer, it looks like this: Correct behavior

This is the right behavior for me.

However, at runtime, it does the following:

Bad behavior

How to make runs run together rather than spaced?

+4
source share
3 answers

OK, after reading a post from Pieter Nijs, I created Behavior for UWP that works for Universal Windows applications.

public class RemoveEmptyRunsBehavior : Behavior<RichTextBlock>
{
    protected override void OnAttached()
    {
        RemoveWhitespaceRuns(this.AssociatedObject);
    }

    private void RemoveWhitespaceRuns(RichTextBlock tb)
    {
        var tuples = from p in tb.Blocks.OfType<Paragraph>()
            from r in p.Inlines.OfType<Run>()
            where r.Text == " "
            select new { Paragraph = p, Run = r };

        foreach (var tuple in tuples)
        {
            tuple.Paragraph.Inlines.Remove(tuple.Run);
        }
    }
}

: RichTextBlock, .

<RichTextBlock VerticalAlignment="Center">
    <Paragraph>
        <Run Text="Hello" />
        <Run Text="world" />
    </Paragraph>

    <interactivity:Interaction.Behaviors>
        <local:RemoveEmptyRunsBehavior />
    </interactivity:Interaction.Behaviors>
</RichTextBlock>

XAML UWP ( NuGet): Microsoft.Xaml.Behaviors.Uwp.Managed

!

xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
0

Pieter Nijs , ! ...

, , , .

var spaces = tb.Inlines.Where(a => a is Run 
        && ((Run)a).Text == " "
        && !GetPreserveSpace(a)).ToList();
    spaces.ForEach(s => tb.Inlines.Remove(s));
+2

, , , , TextBlock Rich ; -).

RichTextBlock (, , , , ).

public class RichTextBlockExtension
{
    public static bool GetRemoveEmptyRuns(DependencyObject obj)
    {
        return (bool)obj.GetValue(RemoveEmptyRunsProperty);
    }

    public static void SetRemoveEmptyRuns(DependencyObject obj, bool value)
    {
        obj.SetValue(RemoveEmptyRunsProperty, value);

        if (value)
        {
            var tb = obj as RichTextBlock;
            if (tb != null)
            {
                tb.Loaded += Tb_Loaded;
            }
            else
            {
                throw new NotSupportedException();
            }
        }
    }

    public static readonly DependencyProperty RemoveEmptyRunsProperty =
    DependencyProperty.RegisterAttached("RemoveEmptyRuns", typeof(bool),
    typeof(RichTextBlock), new PropertyMetadata(false));



    public static bool GetPreserveSpace(DependencyObject obj)
    {
        return (bool)obj.GetValue(PreserveSpaceProperty);
    }

    public static void SetPreserveSpace(DependencyObject obj, bool value)
    {
        obj.SetValue(PreserveSpaceProperty, value);
    }

    public static readonly DependencyProperty PreserveSpaceProperty =
    DependencyProperty.RegisterAttached("PreserveSpace", typeof(bool),
    typeof(Run), new PropertyMetadata(false));


    private static void Tb_Loaded(object sender, RoutedEventArgs e)
    {
        var tb = sender as RichTextBlock;
        tb.Loaded -= Tb_Loaded;

        foreach (var item in tb.Blocks)
        {
            Paragraph p = item as Paragraph;
            if(p!=null)
            {
                var spaces = p.Inlines.Where(a => a is Run
                   && ((Run)a).Text == " "
                   && !GetPreserveSpace(a)).ToList();
                            spaces.ForEach(s => p.Inlines.Remove(s));
            }
        }
    }
}
+2

Source: https://habr.com/ru/post/1670045/


All Articles