WPF StringFormat example in Binding



In this small article we will see how we can use StringFormat when we use Binding.
In the example we have use one Date Picker and one TextBlock. The TextBlock isbound with the SelectedDate property of the DatePicker.And we have used StringFormat property to modify the text shown in TextBlock.

Sample Code:
Here is the code and output when we don’t use StringFormat:
<Grid>
        <StackPanel Orientation="Vertical">
        <DatePicker x:Name="datePicker" Margin="5"/>
            <TextBlock x:Name="textBlock" Margin="5" Text="{Binding ElementName=datePicker,Path=SelectedDate}"/>
        </StackPanel>
    </Grid>








Here is the code and output when we use StringFormat property:
<Grid>
        <StackPanel Orientation="Vertical">
        <DatePicker x:Name="datePicker" Margin="5"/>
            <TextBlock x:Name="textBlock" Margin="5" Text="{Binding ElementName=datePicker,Path=SelectedDate,StringFormat='dd MMM yyyy'}"/>
        </StackPanel>
    </Grid>