I have been playing with WPF
for a while and I came across an interesting thing. When I bind DateTime
object to the Label
's content then I see locally formatted representation of the date. However, when I bind to the TextBlock
's Text property then I actually see English one.
It seems that TextBlock
is using some sort of converter whereas Label
is just calling ToString
method but I am not sure about it.
If so, why doesn't the Label
use the converter too?
Is there any reason it works this way? I provide a short sample to let you guys inspect what's going on:
// MainWindow.xaml
<Window x:Class="BindConversion.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel HorizontalAlignment="Center" Margin="3">
<StackPanel>
<Label Content="{Binding Dt}"/>
<TextBlock Text="{Binding Dt}"/>
</StackPanel>
</StackPanel>
</Window>
// MainWindow.xaml.cs
using System;
using System.Windows;
namespace BindConversion
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public DateTime Dt { get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
Dt = DateTime.Now;
}
}
}
Labels usually support single line text output while the TextBlock is intended for multiline text display. For example in wpf TextBlock has a property TextWrapping which enables multiline input; Label does not have this.
TextField is the lowest level text component provided by the player itself. Label is a wrapper that introduces the functionality provided by the Flash framework and provides read-only text component.
The TextBlock control provides flexible text support for UI scenarios that do not require more than one paragraph of text. It supports a number of properties that enable precise control of presentation, such as FontFamily, FontSize, FontWeight, TextEffects, and TextWrapping.
If you take a closer look to Label
you will see that it derives from ContentControl
.
Content
property is displayed by a ContentPresenter
where in the docs it is said the following:
If there is a TypeConverter that converts the type of Content to a UIElement, the ContentPresenter uses that TypeConverter and the resulting UIElement is displayed.
Now there is a DateTimeConverter
inheriting from TypeConverter
, the following snippet produces exactly the same string than a Label
does:
var dateTimeConverter = new DateTimeConverter();
var convertToString = dateTimeConverter.ConvertToString(DateTime.Now);
References:
https://msdn.microsoft.com/en-us/library/system.windows.controls.contentpresenter(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.componentmodel.datetimeconverter(v=vs.110).aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With