0

Basically in my Windows Phone application, i used to display the Name in the List through Binding. In this case, i want to restrict the name to display only the first 5 set of characters to avoid the unnecessary wrapping.

In my view, We can achieve this using the Converter option while binding the Name to the TextBox. But is there any other option to achieve this through XAML itself using StringFormat option while binding. Could you please anyone help me on this ?

<TextBox Text="{Binding Path=Name, StringFormat=??}" TextWrapping="NoWrap"/>
David Bekham
  • 2,175
  • 3
  • 27
  • 56
  • Looking at this article, makes me think something like `StringFormat=\{0,5\}` would do what you need, although I haven't tried. http://blogs.msdn.com/b/matthiasshapiro/archive/2012/12/11/complete-guide-to-windows-phone-stringformat-binding.aspx – lhan May 22 '13 at 14:52
  • Thanks for the response. I tried this option. But it displays minimum of 5 characters. If we have name Tom means, It comes with " Tom". So it did not helped me. – David Bekham May 22 '13 at 15:03
  • Oh, so `Name` contains first name *and* last name, and you want the full first name + the first 5 characters of the last name? – lhan May 22 '13 at 15:07
  • No. There is no FirstName and LastName, If we have name with more than 5 characters, I should display only first 5 characters using StringFormat. Sometimes the name contains more than 15 or 20 characters means it makes unnecessary wrapping so i want to restrict to 5. – David Bekham May 22 '13 at 15:13
  • Oh I see, so my example would have worked but you'd need a way to trim the beginning spaces if the name is less than 5 characters? – lhan May 22 '13 at 15:22
  • No. From your solution, if we have 10 characters it just displays all the characters. It did not do any SubString option to restrict only to 5. – David Bekham May 22 '13 at 15:27
  • I think that the use of a converter will be necessary. Similar questions point in that direction: http://stackoverflow.com/questions/9176221/substring-a-bound-string http://stackoverflow.com/questions/2006111/wpf-binding-stringformat-to-show-only-first-character – anderZubi May 22 '13 at 15:33
  • Is there any other way to achieve this without using converter ?. That's what my actual question ? – David Bekham May 22 '13 at 18:14

1 Answers1

0

You could use the TextTrimming property of a TextBlock and create a style which contains a TextBlock inside your TextBox, like so:

Style 1

<!-- Trims text but shows all on-focus -->
<Style TargetType="TextBox" x:Key='TrimmingStyle1'>
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsKeyboardFocused, RelativeSource={RelativeSource Self}}" Value="false">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="TextBox">
                        <Border BorderThickness='1' Background='#ffefefef' BorderBrush='LightBlue'>
                            <TextBlock Text="{TemplateBinding Text}" TextTrimming="None" Margin='4,1' />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>

Style 2

<!-- Trims text always, non editable -->
<Style TargetType="TextBox" x:Key='TrimmingStyle2'>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TextBox">
                <Border BorderThickness='1' Background='#ffefefef' BorderBrush='LightBlue'>
                    <TextBlock Text="{TemplateBinding Text}" TextTrimming="None" Margin='4,1' />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Usage

<TextBox Style="{StaticResource TrimmingStyle1}" ... />
<TextBox Style="{StaticResource TrimmingStyle2}" ... />

Be sure to change the text bindings to match your application data. Also note that the trimming depends on the size of your textbox.

Source: Using a Style to Simulate TextTrimming on TextBox

aqua
  • 3,269
  • 28
  • 41