Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rid of whitespace between Runs in TextBlock?

Tags:

wpf

xaml

I have following XAML:

<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center"
                                               FontSize="10" FontFamily="Arial" Foreground="#414141">        
                                            <Run Text="{Binding LoadsCount}" />        
                                            <Run Text="+" />        
                                            <Run Text="{Binding BrokerLoadsCount}" />
                                        </TextBlock>

And I get display like this: 12 + 11 Somehow it inserts extra space between each Run How do I make it display 12+11 ?

like image 932
katit Avatar asked Jun 18 '12 19:06

katit


4 Answers

The spaces between the run tags cause the spaces, this is the easiest fix.

<TextBlock 
   HorizontalAlignment="Center" 
   VerticalAlignment="Center"
   FontSize="10" 
   FontFamily="Arial" 
   Foreground="#414141">        
      <Run Text="{Binding LoadsCount}" /><Run Text="+" /><Run Text="{Binding BrokerLoadsCount}" />
</TextBlock>

Because anything between the <TextBlock> and </TextBlock> is targeting the text property of the TextBlock the whitespace from the breaks between the runs causes the effect you see. You could also shorten it to this.

<Run Text="{Binding LoadsCount}" />+<Run Text="{Binding BrokerLoadsCount}" />

This MSDN article gives all the specifics on how xaml handles the whitespace

http://msdn.microsoft.com/en-us/library/ms788746.aspx

If you were curious why a break and a ton of tabs translates into a single space

All whitespace characters (space, linefeed, tab) are converted into spaces.

All consecutive spaces are deleted and replaced by one space

like image 140
Kevin DiTraglia Avatar answered Oct 18 '22 03:10

Kevin DiTraglia


Another option is to comment the space between Run tags, maintaining the code readable and removing the extra space.

<TextBlock HorizontalAlignment="Center"
           VerticalAlignment="Center"
           FontSize="10" FontFamily="Arial" Foreground="#414141">        
    <Run Text="{Binding LoadsCount}" /><!--
 --><Run Text="+" /><!--
 --><Run Text="{Binding BrokerLoadsCount}" />
</TextBlock>
like image 46
helder.tavares.silva Avatar answered Oct 18 '22 02:10

helder.tavares.silva


One problem with Kevin's nice solution is that the single-line formatting of XAML tags is undone when you apply some of the XAML/XML automatic reformatting functions, e.g. "ctrl-K + ctrl-D". One workaround I found is to format the Run tags as follows:

<TextBlock>
    <Run FontStyle="Italic"
    Text="aaa" /><Run 
    Text="bbb" />
</TextBlock>

Although splitting the tag across lines like this is somewhat awkward, this format will not be altered by automatic reformatting, provided you select the Visual Studio option "Preserve new lines and spaces between attributes" for the XAML text editor:

extra space eliminated between consecutive Run elements in XAML

like image 21
Glenn Slayden Avatar answered Oct 18 '22 03:10

Glenn Slayden


I've written a Attached Property to 'bypass' this behavior.

public class TextBlockExtension
{

    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 TextBlock;
            if (tb != null)
            {
                tb.Loaded += Tb_Loaded;
            }
            else
            {
                throw new NotSupportedException();
            }
        }
    }

    public static readonly DependencyProperty RemoveEmptyRunsProperty =
        DependencyProperty.RegisterAttached("RemoveEmptyRuns", typeof(bool), 
            typeof(TextBlock), 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 TextBlock;
        tb.Loaded -= Tb_Loaded;

       var spaces = tb.Inlines.Where(a => a is Run 
            && string.IsNullOrWhiteSpace(((Run)a).Text) 
            && !GetPreserveSpace(a)).ToList();
        spaces.ForEach(s => tb.Inlines.Remove(s));
    }
}

The entire source code and the explanation of it all can be found here. By using this attached property you can keep your XAML formatting just the way you want, but you don't get these whitespaces in your rendered XAML.

like image 1
Pieter Nijs Avatar answered Oct 18 '22 04:10

Pieter Nijs