Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Culture difference between Label and TextBlock

Tags:

c#

wpf

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;
        }
    }
}
like image 811
bottaio Avatar asked Aug 26 '16 18:08

bottaio


People also ask

What is difference between label and TextBlock in WPF?

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.

What is the difference between TextField and label?

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.

What is a TextBlock in WPF?

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.


1 Answers

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

like image 79
aybe Avatar answered Sep 29 '22 12:09

aybe