标签和文本块之间的文化差异

时间:2022-03-27 14:17:45

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.

我和WPF玩了一段时间,发现了一件有趣的事情。当我将DateTime对象绑定到标签的内容时,我将看到日期的本地格式表示。但是,当我绑定到TextBlock的文本属性时,我实际上看到了英语。

It seems that TextBlock is using some sort of converter whereas Label is just calling ToString method but I am not sure about it.

似乎TextBlock使用某种转换器,而Label只调用ToString方法,但我不确定。

If so, why doesn't the Label use the converter too?

如果是这样,那么标签为什么不使用转换器呢?

Could somebody explain to me why is it working the way it is? 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;
        }
    }
}

2 个解决方案

#1


2  

If you take a closer look to Label you will see that it derives from ContentControl.

如果你仔细看一下标签,你会发现它来自于ContentControl。

Content property is displayed by a ContentPresenter where in the docs it is said the following:

内容属性由ContentPresenter显示,在文档中它是这样说的:

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.

如果有一个将内容类型转换为UIElement的类型转换为类型转换的类型转换程序,那么ContentPresenter将使用该类型转换程序,并显示生成的UIElement。

Now there is a DateTimeConverter inheriting from TypeConverter, the following snippet produces exactly the same string than a Label does:

现在有一个DateTimeConverter从TypeConverter继承,下面的代码片段生成的字符串与一个标签所做的完全相同:

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.windows.controls.contentpresenter(v = vs.110). aspx

https://msdn.microsoft.com/en-us/library/system.componentmodel.datetimeconverter(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.componentmodel.datetimeconverter(v = vs.110). aspx

#2


1  

They are actually quite different beasts.

它们实际上是完全不同的动物。

Check this out for more details: https://joshsmithonwpf.wordpress.com/2007/07/04/differences-between-label-and-textblock/

查看这个信息,了解更多细节:https://joshsmithonwpf.wordpress.com/2007/07/04/04/04/04/04/04/04/04/04/04/04/04/04/07/04/04/04/04/04/04/04/04/04/04/04/04/04/04/04/04/04/04/

Label actually isn't even a control

标签实际上甚至不是控件

#1


2  

If you take a closer look to Label you will see that it derives from ContentControl.

如果你仔细看一下标签,你会发现它来自于ContentControl。

Content property is displayed by a ContentPresenter where in the docs it is said the following:

内容属性由ContentPresenter显示,在文档中它是这样说的:

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.

如果有一个将内容类型转换为UIElement的类型转换为类型转换的类型转换程序,那么ContentPresenter将使用该类型转换程序,并显示生成的UIElement。

Now there is a DateTimeConverter inheriting from TypeConverter, the following snippet produces exactly the same string than a Label does:

现在有一个DateTimeConverter从TypeConverter继承,下面的代码片段生成的字符串与一个标签所做的完全相同:

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.windows.controls.contentpresenter(v = vs.110). aspx

https://msdn.microsoft.com/en-us/library/system.componentmodel.datetimeconverter(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.componentmodel.datetimeconverter(v = vs.110). aspx

#2


1  

They are actually quite different beasts.

它们实际上是完全不同的动物。

Check this out for more details: https://joshsmithonwpf.wordpress.com/2007/07/04/differences-between-label-and-textblock/

查看这个信息,了解更多细节:https://joshsmithonwpf.wordpress.com/2007/07/04/04/04/04/04/04/04/04/04/04/04/04/04/07/04/04/04/04/04/04/04/04/04/04/04/04/04/04/04/04/04/04/

Label actually isn't even a control

标签实际上甚至不是控件