StringFormat是否与this.DataContext一起使用

时间:2011-01-25 06:36:10

标签: wpf xaml string-formatting

我通过this.DataContext = SellerList将TextBlock与代码隐藏中的集合绑定在一起。 输出是正确的,但是当我应用StringFormat时,我看不到任何结果。以下是xaml页面上TextBlock的代码

<TextBlock Name="dateDTKey" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Right" VerticalAlignment="Bottom" 
      Text="{Binding Path=Date, StringFormat={}{0:dd-MM-yyyy}}" 
      Style="{StaticResource textStyleTextBlock}"/>

2 个答案:

答案 0 :(得分:1)

Binding的来源是一个字符串,如果detailsSellerListingTemplate是您应该使用{StaticResource detailsSellerListingTemplate}的资源。此外,TextBlock不需要DataContext来使用此Binding,因为它使用Source。

<Window.Resources>
    <local:DetailsSeller x:Key="detailsSellerListingTemplate"/>
</Window.Resources>
<TextBlock Name="dateDTKey"
           HorizontalAlignment="Right"
           VerticalAlignment="Bottom"
           Text="{Binding Source={StaticResource detailsSellerListingTemplate},
                          Path=Date,
                          StringFormat={}{0:dd-MM-yyyy}}"/>

如果DetailsSeller看起来类似于此

,这将有效
public class DetailsSeller
{
    public DetailsSeller()
    {
        Date = DateTime.Now;
    }

    public DateTime Date
    {
        get;
        set;
    }
}

你谈到了一个集合,但我看不出它如何与绑定相符,所以也许我很想念问题中的内容

答案 1 :(得分:0)

我在想它,因为你的字符串格式有很多大括号。试试这个:

StringFormat={0:dd-MM-yyyy}