如何在XAML中格式化TimeSpan

bij*_*iju 88 c# data-binding wpf xaml string-formatting

我正在尝试格式化绑定到TimeSpan属性的文本块.如果属性是类型DateTime但它失败,如果它是a,它可以工作TimeSpan.我可以使用转换器完成它.但我试图找出是否有其他选择.

示例代码:

public TimeSpan MyTime { get; set; }

public Window2()
{
    InitializeComponent();
    MyTime = DateTime.Now.TimeOfDay;
    DataContext = this;
}
Run Code Online (Sandbox Code Playgroud)

XAML

<TextBlock Text="{Binding MyTime,StringFormat=HH:mm}"/>
Run Code Online (Sandbox Code Playgroud)

我希望文本块只显示小时和分钟.但它显示为:

19:10:46.8048860

Tim*_*oyd 136

格式字符串旨在用于a DateTime而不是a TimeSpan.

您可以改为使用代码DateTime.Now.你的xaml很好:

<TextBlock Text="{Binding MyTime,StringFormat=HH:mm}"/>
Run Code Online (Sandbox Code Playgroud)

更新

.Net 4格式a TimeSpan如下:

<TextBlock Text="{Binding MyTime,StringFormat=hh\\:mm}"/>
Run Code Online (Sandbox Code Playgroud)

  • 我不知道我做错了什么..但它仍然不适合我.VisualStudio 2008,Framework 3.5 (2认同)

Fre*_*lad 89

在.NET 3.5中,您可以使用MultiBinding

<TextBlock>
    <TextBlock.Text>
        <MultiBinding StringFormat="{}{0}:{1}">
            <Binding Path="MyTime.Hours"/>
            <Binding Path="MyTime.Minutes"/>
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)

更新
要回答评论.

要确保输出2位数,即使小时或分钟为0-9,您也可以使用{0:00}而不是{0}.这将确保时间12:01的输出为12:01而不是12:1.
如果要输出01:01作为1:01使用StringFormat="{}{0}:{1:00}"

并且条件格式可用于删除分钟的负号.而不是{1:00}我们可以使用{1:00; 00}

<TextBlock>
    <TextBlock.Text>
        <MultiBinding StringFormat="{}{0:00}:{1:00;00}">
            <Binding Path="MyTime.Hours" />
            <Binding Path="MyTime.Minutes" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)


Cyg*_*gon 42

只是为了添加到池中,我成功地使用此绑定在生产WPF应用程序中显示TimeSpan:

Binding="{Binding Time, Mode=TwoWay, StringFormat=\{0:h\\:mm\}}"
Run Code Online (Sandbox Code Playgroud)

采取一些尝试得到正确的反斜杠:)

  • 如果你想要分数秒,你也需要逃避逗号:{0:hh \\:mm \\:ss \\.ffff} (5认同)
  • 这也非常适合Xamarin Forms xaml。 (2认同)
  • StringFormat=\{0:hh\\:mm\\:ss\\.fff\} (2认同)

Pet*_*old 22

StringFormat必须采用格式字符串的形式.在这种情况下,它看起来像:

<TextBlock Text="{Binding MyTime,StringFormat=`Time values are {0:hh\\:mm}`}"/>
Run Code Online (Sandbox Code Playgroud)

注意:如果您想显示总小时数和分钟数,且时间跨度恰好超过24小时,那么您的方法有一个警告:这是一种解决方法.

  • 另请注意,这适用于TextBlock但不带(!)带有Label. (2认同)
  • 仅当Binding的目标是String类型的属性时,才使用@Shackles StringFormat.Label.Content是Object,因此被忽略.TextBlock.Text是一个字符串,因此使用它. (2认同)

juF*_*uFo 15

对于Multi绑定,您需要注意.NET 4以来的注意事项.

下面简要概述,使用.NET 4.6进行测试:

定期绑定:

<TextBlock Text="{Binding Start, StringFormat='{}{0:hh\\:mm\\:ss}'}" />
Run Code Online (Sandbox Code Playgroud)

多重绑定:

<TextBlock.Text>
    <MultiBinding StringFormat="{}{0:hh':'mm':'ss} -> {1:hh':'mm':'ss}">
        <Binding Path="Start" Mode="OneWay" UpdateSourceTrigger="PropertyChanged" />
        <Binding Path="End" Mode="OneWay" UpdateSourceTrigger="PropertyChanged" />
    </MultiBinding>
</TextBlock.Text>
Run Code Online (Sandbox Code Playgroud)

或者您可以在多重绑定中使用"而不是":

<MultiBinding StringFormat='{}{0:hh":"mm":"ss} -> {1:hh":"mm":"ss}'>
Run Code Online (Sandbox Code Playgroud)

注意:使用StringFormat ="{} {0:hh \:\:mm \:ss} - > {1:hh \:mm \:ss}"无法在MultiBinding上运行,这将导致空白结果.


小智 12

.NET 4中的WPF现在有来自字符串的时间跨度http://msdn.microsoft.com/en-us/library/ee372286.aspx

我使用以下内容 <TextBlock FontSize="12" Text="{Binding Path=TimeLeft, StringFormat={}{0:g}}" />


She*_*dan 11

我知道这个问题现在已经过时了,但我很惊讶没有人建议这个简单直接StringFormat适用于TimeSpan:

<TextBlock Text="{Binding MyTime, StringFormat={}{0:hh}:{0:mm}, FallbackValue=00:00}"/>
Run Code Online (Sandbox Code Playgroud)

  • 这周围有很多混乱.似乎可能是.NET 4改变了语法. (2认同)

小智 8

如果要在使用Content属性的Label中使用StringFormat,可以使用ContentStringFormat格式化时间跨度:

<Label Content={Binding MyTimespan}" ContentStringFormat="{}{0:hh}:{0:mm}:{0:ss}"
Run Code Online (Sandbox Code Playgroud)

  • 您在答案中声明的内容值得双重突出显示:在使用“Content”属性的 XAML 控件中(即,不是“Text”属性,例如“TextBlock”),必须使用属性“ContentStringFormat”。将“StringFormat”指定为绑定的一部分将不起作用。 (2认同)

Ste*_*nko 5

带毫秒的 TimeSpan StringFormat:

<TextBlock Text="{Binding MyTime, StringFormat=\{0:hh\\:mm\\:ss\\.fff\}}"/>
Run Code Online (Sandbox Code Playgroud)