WPF XAML日期时间字符串的格式化

2019-10-14  本文已影响0人  达哥傻乐

XAML中的日期格式化跟程序代码中的格式化很类似,但有一些小小的不同:

对于某些控件我们可以使用ContentStringFormat

<Label
    Content="{Binding Path=BirthDate}"
    ContentStringFormat="{}{0:dd MM YYYY HH:mm:ss}" />
<Label
    Content="{Binding Path=BirthDate}"
    ContentStringFormat="{}{0:d}" />
<Label
    Content="{Binding Path=BirthDate}"
    ContentStringFormat="{}{0:t}" />
<Label
    Content="{Binding Path=BirthDate}" 
/>

对于很多控件我们可以在绑定时使用StringFormat

  <ListView.View>
    <GridView AllowsColumnReorder="True">
        <GridViewColumn x:Name="colBirthDate" Header="出生日期" 
                        DisplayMemberBinding="{Binding Path=BirthDate, StringFormat='{}{0:t}'}"    
                        Width="Auto"/>
    </GridView>
</ListView.View>
<TextBlock Text="{Binding Path=BirthDate, StringFormat={}{0:MM/dd/yyyy}}" />
<TextBlock Text="{Binding Path=BirthDate, StringFormat={}{0:MM/dd/yyyy hh:mm tt}}" />
<TextBlock Text="{Binding Path=Price, StringFormat={}{0:C}}" />
<TextBlock Text="{Binding Path=Price, StringFormat=价格:{0:C}}" />
<Button Content="Delete Me">
    <Button.ToolTip>
        <ToolTip>
            <StackPanel Orientation="Horizontal">
                <TextBlock>
                    <TextBlock.Text>
                        <MultiBinding StringFormat="Delete {0} {1}">
                            <Binding Path="FirstName" />
                            <Binding Path="LastName" />
                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>

鸣谢:

WPF String.Format in XAML with the StringFormat attribute
XAML Using String Format

2020-04-01 无意间看到此文,大善,附在此处:

WPF中XAML中使用String.Format格式化字符串示例

达叔傻乐(darwin.zuo@163.com)

上一篇 下一篇

猜你喜欢

热点阅读