How WPF Knowledge Points-Binding. StringFormat does not work

Source: Internet
Author: User

Issue

Let's use a simple example to illustrate the problem: there is a DateTime resource that displays the DateTime with TextBox and Label respectively.

<Grid>
<Grid.Resources>
<sys:DateTime x:Key="DateTime001">03/29/2012 15:05:30</sys:DateTime>
</Grid.Resources>
<TextBox Text="{Binding Source={StaticResource DateTime001},StringFormat=dddd , Mode=OneWay}"
Height="23" HorizontalAlignment="Left" Margin="28,68,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<Label Content="{Binding Source={StaticResource DateTime001}, StringFormat=dddd, Mode=OneWay}"
Height="28" HorizontalAlignment="Left" Margin="28,26,0,0" Name="label1" VerticalAlignment="Top" Width="120" />
</Grid>

Running result

The TextBox shows the complete English week as expected, but the Label format has not changed. We use the exactly same Binding and format strings. What is the difference? If you are careful enough, you can find that the Binding of TextBox is performed on the Text attribute, while the Binding of Label is performed on the Content attribute.

 

Detailed analysis

Essentially, Control. Content is of the Object type, and Binding. StringFormat is valid only when the Property type of Binding is string.

Through the Binding process of the Label below (from Stackoverflow), we can see the underlying details:
1.Bind the DateTime type value to the Label. Content.
2.The Template of the Label contains ContentPresenter, which is used to display the content.
3.The Label ContentPresenter searches for ContentTemplate and DataTemplate to display the content. If none of the content is found, the Label uses the default Template.
4.The default Template used by ContentPresenter uses the Label. ContentStringFormat attribute to format the object to the string.
5.Note that the above is a simplified process. In essence, ContentPresenter will use its own Template and StringFormat to display the result, the ContentTemplate and ContentStringFormat of the Label are automatically bound to the ContentPresenter's ContentTemplate and StringFormat. In essence, ContentPresenter will first display the ContentTemplate and ContentStringFormat of Label. Therefore, here we can say that CotentPresenter uses the Label Properties for display.

Therefore, for non-String Content, add the attribute definition ContentStringFormat = dddd to display the expected results.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.