Click here to Skip to main content
14,355,397 members

String Format ValueConverter

Rate this:
5.00 (6 votes)
Please Sign up or sign in to vote.
5.00 (6 votes)
9 Aug 2015CPOL
This tip provides an alternate way to combine data with text in a WPF control using a ValueConverter that could be considered easier.

Introduction

I am always looking at ways to reduce maintenance and work, and when I was required to put a header on a tile that included the count of records contained in the tile I immediately decided to create a ValueConverter that was bound to the ItemsSource of the ListBox (or Grid), and took a string that indicated how to combine the count into a string in the ConverterParameter. Since the easiest way to insert the count of items in an arbitrary string is use the string.Format method, this determined how to format the string for the ValueConverter. It also has the advantage in that it is usually easy to include in XAML without special formatting and programmers are familiar with using the string.Format method.

XAML Usage and Comparison

If one wants to compose a string using bound values in WPF, a lot of elements are required:

<WrapPanel Grid.Row="3" Orientation="Horizontal">
  <TextBlock Text="This is the harder way to say that 'User "/>
  <TextBlock Text="{Binding UserName}"/>
  <TextBlock Text=" has "/>
  <TextBlock Text="{Binding Count}"/>
  <TextBlock Text=" records'"/>
</WrapPanel>

This is fairly verbose, and requires a lot of visual elements. If the converter is used for the same thing, it is about as verbose, but much easier to understand:

<TextBlock Grid.Row="2" Grid.Column="0">
  <TextBlock.Text>
    <MultiBinding Converter="{local:StringFormatConverter}"
             ConverterParameter="User {1} has {0} records">
      <Binding Path="Children" />
      <Binding Path="UserName" />
    </MultiBinding>
  </TextBlock.Text>
</TextBlock>

Relatively speaking, having only two arguments is probably the worst case since a single argument could use a standard ValueConverter, and would not require the MultiConverter; the MultiConveter requires the binding to be broken out into elements. However, each extra argument only adds a line. The layout also makes it easier to understand since the format string is in a single attribute, and the bindings are together. The disadvantage is that no longer have the control over formatting each Visual Element.

Note on Collections

The converter will return the count of records if bound to a generic IEnumerable collection. It will also return the count of records for a DataTable. However, unless the PropertyChanged event is executed for the collection property, the count will not be updated. This is even true for an ObservableCollection. Not sure about the reason since a control that handles a list will be updated.

History

08/09/15: Initial version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Clifford Nelson
Software Developer (Senior) Clifford Nelson Consulting
United States United States
Has been working as a C# developer on contract for the last several years, including 3 years at Microsoft. Previously worked with Visual Basic and Microsoft Access VBA, and have developed code for Word, Excel and Outlook. Started working with WPF in 2007 when part of the Microsoft WPF team. For the last eight years has been working primarily as a senior WPF/C# and Silverlight/C# developer. Currently working as WPF developer with BioNano Genomics in San Diego, CA redesigning their UI for their camera system. he can be reached at qck1@hotmail.com.

Comments and Discussions

 
QuestionWhy special-case the parameter conversion? Pin
John Brett9-Aug-15 21:40
memberJohn Brett9-Aug-15 21:40 
AnswerRe: Why special-case the parameter conversion? Pin
Clifford Nelson20-Aug-15 3:22
mvaClifford Nelson20-Aug-15 3:22 
SuggestionWhy not just do it this way? Pin
jyrka989-Aug-15 10:35
memberjyrka989-Aug-15 10:35 
AnswerRe: Why not just do it this way? Pin
Clifford Nelson9-Aug-15 11:12
mvaClifford Nelson9-Aug-15 11:12 
AnswerRe: Why not just do it this way? Pin
Clifford Nelson9-Aug-15 16:21
mvaClifford Nelson9-Aug-15 16:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Tip/Trick
Posted 9 Aug 2015

Stats

7.9K views
6 bookmarked