2012-07-08 84 views
1
<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <System:DateTime x:Key="d" >2012/7/8</System:DateTime> 
    </Window.Resources> 
    <StackPanel> 
     <ContentControl Content="{Binding}" DataContext="{StaticResource d}" /> 
     <TextBlock Text="{Binding}" DataContext="{StaticResource d}"/> 
    </StackPanel> 
</Window> 

此代码给我下面的窗口。相同的绑定,不同的演示文稿?

enter image description here

奇怪的是,当应用到所述ContentControl中相同结合只显示日期部分,并且当应用于TextBlock中还示出了时间的一部分。

我只是好奇地知道原因,并问是否可以交换演示文稿,我的意思是TextBlock只显示日期部分和ContentControl显示两部分。

谢谢。

回答

3

对于ContentControl,请使用ContentStringFormat。对于TextBlock,使用BindingStringFormat

<ContentControl Content="{Binding}" ContentStringFormat="dd/MM/yyyy HH:mm:ss"/> 
<TextBlock Text="{Binding ., StringFormat=dd/MM/yyyy}"/> 

总之,差异归结为一个事实,即ContentControl可以显示任何旧object的内容(不只是一个string),而TextBlock.Text只能是string

+0

你的回答太快了!我几乎读完了自己的帖子。谢谢。 – Gqqnbig 2012-07-08 11:59:45