wpf 用于在 XAML 中绑定 TimeSpan 的 StringFormat 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20192430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
StringFormat for binding TimeSpan in XAML does not work
提问by mmvsbg
I have a TimeSpan variable called Time which I managed to successfully bind in XAML. Here's the code that I have:
我有一个名为 Time 的 TimeSpan 变量,我设法在 XAML 中成功绑定了它。这是我拥有的代码:
...
<TextBlock Grid.Column="2" Text="{Binding Time, StringFormat='{}{0:hh:mm}'}" />
...
The values are now displayed properly, however, the format is still hh:mm:ss. I'm trying to get rid of the seconds and display only the hours and the minutes but for some reason the changes that I make to the StringFormat are not accounted for and I always get the same hh:mm:ss format. Can you please advise?
这些值现在可以正确显示,但是格式仍然是 hh:mm:ss。我试图摆脱秒,只显示小时和分钟,但由于某种原因,我对 StringFormat 所做的更改没有考虑在内,我总是得到相同的 hh:mm:ss 格式。你能给些建议么?
Thank you!
谢谢!
EDIT:
编辑:
Judging by the comments below it seems that the Time that I have binded is not in TimeSpan format as the StringFormat seems to be correct but the displayed value is wrong. The Time variable is defined as follows in a custom class Record:
从下面的评论来看,我绑定的时间似乎不是 TimeSpan 格式,因为 StringFormat 似乎是正确的,但显示的值是错误的。Time 变量在自定义类 Record 中定义如下:
private TimeSpan time;
public TimeSpan Time
{
get { return time; }
set { time = value; }
}
In my code I have an observable collection defined:
在我的代码中,我定义了一个可观察的集合:
ObservableCollection<Record> records = new ObservableCollection<Record>();
The collection is the populated an then I have:
该集合是填充的,然后我有:
listBoxRecords.ItemsSource = this.records;
Here's the full XAML, keeping in mind that the rest of the fields are correctly populated, only the TimeSpan is giving me troubles:
这是完整的 XAML,请记住,其余字段已正确填充,只有 TimeSpan 给我带来了麻烦:
<ListBox Margin="0,44,0,58" Name="listBoxRecords" ItemsSource="{Binding records}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120" />
<ColumnDefinition Width="85" />
<ColumnDefinition Width="55" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Date, StringFormat='{}{0:dd-MM-yyyy HH:mm}'}" />
<TextBlock Grid.Column="1" Text="{Binding Type }" />
<TextBlock Grid.Column="2" Text="{Binding Time, StringFormat='{}{0:hh\:mm}'}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
回答by Sheridan
You can get that output by using this StringFormat:
您可以使用以下命令获得该输出StringFormat:
<TextBlock Text="{Binding Time, StringFormat={}{0:hh}:{0:mm}, FallbackValue=00:00}" />
UPDATE >>>
更新 >>>
The first point that I'd like to make is that hhwill display hours in a 12 hour format and you should use HHif you want 24 hour format.
我想说的第一点hh是以 12 小时格式显示小时数,HH如果您想要 24 小时格式,则应使用。
The second point that I'd like to make is that you are clearly confused as to what you are doing in your application. The time value that you are seeing with the seconds value is not coming from this TextBlockand probably not coming from your one either. I think that you need to take a good look at your code... you musthave another TextBlockor something.
我想说明的第二点是,您显然对自己在申请中所做的事情感到困惑。您使用秒值看到的时间值不是来自于此TextBlock,也可能不是来自您的时间值。我认为你需要好好看看你的代码......你必须有另一个TextBlock或其他东西。
回答by dkozl
Try escaping :by changing your StringFormatto something like:
尝试:通过将您更改StringFormat为以下内容来逃避:
StringFormat='{}{0:hh\:mm}'
回答by TaterJuice
None of the proposed answers worked for me, they all produces binding errors. I ended up making a sample app to test every possible string format found on MSDN: Custom TimeSpan Format Strings
建议的答案都不适合我,它们都会产生绑定错误。我最终制作了一个示例应用程序来测试MSDN 上发现的每种可能的字符串格式:自定义时间跨度格式字符串
I found from this testing that only the TimeSpan parts which start with %(like %dfor Days) worked in WPF StringFormatproperty.
我从这个测试中发现,只有以%(如%dDays)开头的 TimeSpan 部分在 WPFStringFormat属性中有效。
Try this, it worked for me:
试试这个,它对我有用:
<TextBlock
Text="{Binding Path=Time,
StringFormat={}{0:%d} days {0:%h} hours {0:%m} minutes,
FallbackValue=0 days 0 hours 0 minutes}" />
回答by Maurizio momix Verde
from .Net 4 for timespan you can use
来自 .Net 4 的时间跨度,您可以使用
<TextBlock Grid.Column="2" Text="{Binding Time, StringFormat=hh\:mm\:ss}" />
as for tim-lloyd answer in How to format TimeSpan in XAML
至于如何在 XAML 中格式化 TimeSpan 中的tim-lloyd 答案
Mark as answer if it worked.
如果有效,请标记为答案。
Regards
问候
回答by Richard Suarez
Searched numerous threads with this problem i found that: As of July 4th, 2019 this works for me in XAML in Xamarin forms:
搜索了许多有关此问题的线程,我发现:截至 2019 年 7 月 4 日,这对我在 Xamarin 形式的 XAML 中有效:
Text="{Binding myTimeSpan, StringFormat=\{0:h\\:mm\\:ss\}, FallbackValue=00:00:00}
Text="{Binding myTimeSpan, StringFormat=\{0:h\\:mm\\:ss\}, FallbackValue=00:00:00}
It displays time in hour, minutes, seconds:
它以小时、分钟、秒显示时间:
1 hour 23 minutes and 9 seconds = 1:23:09
1 小时 23 分 9 秒 = 1:23:09
回答by CINCHAPPS
For those interested in a programmatic approach, this worked for me:
对于那些对程序化方法感兴趣的人,这对我有用:
{
DataGridTextColumn dgtc = new DataGridTextColumn();
dgtc.Header = "End Time";
dgtc.Binding = new Binding("EndTime");
dgtc.Binding.StringFormat = "{0:hh}:{0:mm}";
ScheduleGrid.Columns.Add(dgtc);
}

