Search code examples
c#.netwpfstring-formattingmultibinding

Programmaticaly How to provide consecutive arbitary number of whitespace characters within MultiBinding StringFromat?


Please examine following code snippet :

<TextBox x:Name="TxtBox_CommandInfo"  Style="{DynamicResource MetroTextBox}" IsReadOnly="True" Controls:TextBoxHelper.Watermark="This is a textbox" HorizontalAlignment="Left" Margin="0,236,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="154" Width="780" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto">
        <TextBox.Text>
            <MultiBinding StringFormat="{}{0}      {1}        {2} {3}&#x20;{4} {5}    {6}          {7}          {8}        {9}        {10}&#x0a;{11}">
                <Binding  Path="No" />
                <Binding  Path="SelectedType" />
                <Binding  Path="IgnoreSignature"/>
                <Binding  Path="IgnoreRelString"/>
                <Binding  Path="DirectDL"/>
                <Binding  Path="LegacySC" />
                <Binding  Path="SelectedDVName"/>
                <Binding  Path="SelectedHCName"/>
                <Binding  Path="SelectedSCName"/>
                <Binding  Path="SCRelease"/>
                <Binding  Path="DALName"/>
                <Binding  Path="PreviousLogs"/>
            </MultiBinding>
        </TextBox.Text>
    </TextBox>

Here I had to hard-code number of consecutive white-space characters whenever I wanted to format the spaces between bind parameters.

  • Using &#x20; (Code for white-space) ,Can we format the string in programmatical/convenient manner(with tricky code) rather than hard-coding(typing) repetitive characters(number of consecutive white spaces that we want).

My assumption like :

<MultiBinding StringFormat="{}{0}[&#x20;,4]{1}[&#x20;,8]{2}{3}{4}[&#x20;,6]{5}{6}[&#x20;,10]{7}{8}{9}{10}&#x0a;{11}"

  • Therefore is there any valid mechanism under WPF/XAML MultiBinding StringFormat to provide that, for a given character or set of characters "How many number of consecutive characters or how many repetition of characters should be generated ?"

Means Rather than typing required white spaces like this:

<MultiBinding StringFormat="{}{0} {1} {2}" />

Alternative/Programmatical/Convenient but assumed mechanism(to provide how many number of whitespaces we want) like this:

<MultiBinding StringFormat="{}{0}[Character/s,Number of Repetition]{1}[Character/s,Number of Repetition]{2}" /> <!-- Assumed code like --> <MultiBinding StringFormat="{}{0}[&#x20,8]{1}[&#x20,10]{2}" />

Thank you!


Solution

  • You can add a whitespace argument and pad it:

    <MultiBinding StringFormat="{}{0}{12,5}{1}{12,5}{2}{12,1}{3}{12,1}{4}{12,1}{5}{12,4}{6}{12,8}{7}{12,8}{8}{12,8}{9}{12,4}{10}&#x0a;{11}">
        <Binding Path="No" />
        <Binding Path="SelectedType" />
        <Binding Path="IgnoreSignature"/>
        <Binding Path="IgnoreRelString"/>
        <Binding Path="DirectDL"/>
        <Binding Path="LegacySC" />
        <Binding Path="SelectedDVName"/>
        <Binding Path="SelectedHCName"/>
        <Binding Path="SelectedSCName"/>
        <Binding Path="SCRelease"/>
        <Binding Path="DALName"/>
        <Binding Path="PreviousLogs"/>
        <Binding Path="SomeBindingPathThatDoesNotExist" FallbackValue=" "/>
    </MultiBinding>
    

    {12} would be your value (the fallback value which is a space), and you can pad it with {12,x}

    A better yet solution would be padding each of the bindings. That gives you a fixed size for each binding (doesn't add a constant number of whitespaces), but it's usually what you want:

    <MultiBinding StringFormat="{}{0,10}{1,10}{2,8}(etc.)">
        <Binding Path="No" />
        <Binding Path="SelectedType" />
        <Binding Path="IgnoreSignature"/>
        <Binding Path="IgnoreRelString"/>
        <Binding Path="DirectDL"/>
        <Binding Path="LegacySC" />
        <Binding Path="SelectedDVName"/>
        <Binding Path="SelectedHCName"/>
        <Binding Path="SelectedSCName"/>
        <Binding Path="SCRelease"/>
        <Binding Path="DALName"/>
        <Binding Path="PreviousLogs"/>
    </MultiBinding>