Here’s a comprehensive post on String Formatting in WPF (Windows Presentation Foundation) using C#. This article is suitable for developers who want to understand how to properly format strings in WPF UI elements like TextBlock
, Label
, TextBox
, and within bindings.
String Formatting in WPF Using C#
Getting Started
String formatting is a fundamental part of displaying data in a user-friendly way. In WPF (Windows Presentation Foundation), C# provides several mechanisms to format strings, whether you're binding data to UI elements or displaying calculated values.
This article covers:- Basic string formatting with String.Format()
- String formatting in XAML data bindings
- Using StringFormat in Binding
- Custom format strings
- Culture-aware formatting
Basic C# Format String Example
In regular C# code (e.g., code-behind or view model), the most common way to format strings is with String.Format()
or string interpolation.
int age = 20;
string name = "Raghav";
string message = String.Format("Name: {0}, Age: {1}", name, age);
// OR using string interpolation
string message2 = $"Name: {name}, Age: {age}";
Console.WriteLine(message2);
Output
Name: Raghav, Age: 20
These methods are often used in the logic layer, but when building UIs with WPF, we typically use bindings and format strings directly in XAML.
String Formatting in WPF XAML Bindings
WPF uses data binding to connect UI elements to data sources. You can format these bound values using the StringFormat
property in XAML.
<TextBlock Text="{Binding Price, StringFormat='Price: {0:C}'}" />
In this example:
Price
is a numeric property from the data context.{0:C}
formats it as currency based on the current culture.
(')
around the format string if it contains curly braces in XAML.
Common Format Strings in WPF
Format | Description | Example |
---|---|---|
{0:C} |
Currency | $10.00 |
{0:P} |
Percent | 90% |
{0:N2} |
Number with 2 decimals | 1,234.57 |
{0:D} |
Long date | Sunday, October 12, 2025 |
{0:MM/dd/yyyy} |
Custom date | 10/12/2025 |
Formatting Dates in WPF
<TextBlock Text="{Binding CreatedDate, StringFormat='Created on: {0:MMMM dd, yyyy}'}" />
Output
Created on: October 12, 2025
Culture-Aware Formatting
WPF respects the culture settings of the current thread or application. This is especially useful for formatting dates, currency, and numbers.
You can explicitly set the culture in App.xaml.cs:CultureInfo culture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
Or you can use a Binding.ConverterCulture in XAML:
<TextBlock Text="{Binding Price, StringFormat='Price: {0:C}', ConverterCulture='fr-FR'}" />
Formatting Multiple Bindings with MultiBinding
If you want to format multiple bound properties, use a MultiBinding
and a StringFormat
.
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="Name: {0}, Age: {1}">
<Binding Path="Name"/>
<Binding Path="Age"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Format in Code-Behind
If you need to apply formatting dynamically:Binding binding = new Binding("Price")
{
StringFormat = "Price: {0:C}"
};
textBlock.SetBinding(TextBlock.TextProperty, binding);
Tips:
- Use
StringFormat
inside bindings to avoid extra code. - Handle culture with
ConverterCulture
or global settings. - Use
MultiBinding
for complex string compositions.
Summary
String formatting in WPF is powerful and flexible. Whether you're showing prices, dates, or complex messages, using StringFormat
in XAML keeps your UI clean and responsive to culture and localization.
Thanks