为什么 XAML 上的 TimeSpan 具有不同的字符串格式?

发布于 2024-12-07 15:45:57 字数 549 浏览 3 评论 0原文

我要疯了。有人可以解释一下为什么这些格式化相同内容的字符串格式如此不同吗?

<DataGridTextColumn Header="Max Time" IsReadOnly="True" Binding="{Binding MaxTime, StringFormat=hh\\:mm\\:ss, TargetNullValue=---}"> 

<DataGridTextColumn Header="Min Time" IsReadOnly="True">
    <DataGridTextColumn.Binding>
        <Binding Path="MinTime" StringFormat="{}{0:hh':'mm':'ss}" TargetNullValue=" --- "/>
    </DataGridTextColumn.Binding>
 </DataGridTextColumn>

当然,其中一个对另一个不起作用。

编辑:我使用 WPF 的次数越多,我就越觉得它不是一个足够成熟的产品。

I'm going crazy. Can someone explain me why these string formats formatting the same thing are so different?

<DataGridTextColumn Header="Max Time" IsReadOnly="True" Binding="{Binding MaxTime, StringFormat=hh\\:mm\\:ss, TargetNullValue=---}"> 

<DataGridTextColumn Header="Min Time" IsReadOnly="True">
    <DataGridTextColumn.Binding>
        <Binding Path="MinTime" StringFormat="{}{0:hh':'mm':'ss}" TargetNullValue=" --- "/>
    </DataGridTextColumn.Binding>
 </DataGridTextColumn>

Of course each one do not work on the other.

EDIT: The more I work with WPF the more I feel it's not a mature enought product.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏日浅笑〃 2024-12-14 15:45:57

我不是格式化 TimeSpan 的专家,因此我无法确切地告诉您为什么它们会产生相同的结果,但您可以在这里阅读相关内容:自定义 TimeSpan 格式字符串

当然,其中一个对另一个不起作用。

它们的工作方式相同,只是您应该在双引号内使用一个反斜杠。以下

<Binding Path="MinTime"
         StringFormat="hh\\:mm\\:ss"
         TargetNullValue=" --- "/>

结果为 hh\\\\:mm\\\\:ss。因此,您应该编写

<Binding Path="MinTime"
         StringFormat="hh\:mm\:ss"
         TargetNullValue=" --- "/>

以下两个 Bindings 应该产生相同的结果

<DataGridTextColumn Header="Max Time" IsReadOnly="True"
                    Binding="{Binding Path=MaxTime,
                                      StringFormat=hh\\:mm\\:ss,
                                      TargetNullValue=' --- '}"/>
<DataGridTextColumn Header="Min Time" IsReadOnly="True">
    <DataGridTextColumn.Binding>
        <Binding Path="MinTime"
                 StringFormat="hh\:mm\:ss"
                 TargetNullValue=" --- "/>
    </DataGridTextColumn.Binding>
</DataGridTextColumn>

,以下两个也应该如此

<DataGridTextColumn Header="Max Time" IsReadOnly="True"
                    Binding="{Binding Path=MaxTime,
                                      StringFormat={}{0:hh':'mm':'ss},
                                      TargetNullValue=' --- '}"/>
<DataGridTextColumn Header="Min Time" IsReadOnly="True">
    <DataGridTextColumn.Binding>
        <Binding Path="MinTime"
                 StringFormat="{}{0:hh':'mm':'ss}"
                 TargetNullValue=" --- "/>
    </DataGridTextColumn.Binding>
</DataGridTextColumn>

I'm no expert in formatting TimeSpan so I can't tell you exactly why they produce the same result but you can read up about it here: Custom TimeSpan Format Strings

Of course each one do not work on the other.

They do work the same way, the thing is just that you should use one backslash within the double quotes. The following

<Binding Path="MinTime"
         StringFormat="hh\\:mm\\:ss"
         TargetNullValue=" --- "/>

comes out to hh\\\\:mm\\\\:ss. So instead you should write

<Binding Path="MinTime"
         StringFormat="hh\:mm\:ss"
         TargetNullValue=" --- "/>

The following two Bindings should produce the same result

<DataGridTextColumn Header="Max Time" IsReadOnly="True"
                    Binding="{Binding Path=MaxTime,
                                      StringFormat=hh\\:mm\\:ss,
                                      TargetNullValue=' --- '}"/>
<DataGridTextColumn Header="Min Time" IsReadOnly="True">
    <DataGridTextColumn.Binding>
        <Binding Path="MinTime"
                 StringFormat="hh\:mm\:ss"
                 TargetNullValue=" --- "/>
    </DataGridTextColumn.Binding>
</DataGridTextColumn>

And so should the following two

<DataGridTextColumn Header="Max Time" IsReadOnly="True"
                    Binding="{Binding Path=MaxTime,
                                      StringFormat={}{0:hh':'mm':'ss},
                                      TargetNullValue=' --- '}"/>
<DataGridTextColumn Header="Min Time" IsReadOnly="True">
    <DataGridTextColumn.Binding>
        <Binding Path="MinTime"
                 StringFormat="{}{0:hh':'mm':'ss}"
                 TargetNullValue=" --- "/>
    </DataGridTextColumn.Binding>
</DataGridTextColumn>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文