如何在 XAML 元素中使用 StringFormat?
我深入研究了绑定到订单的 XAML 元素堆栈。
订单日期显示为“12/31/2008 12:00:00 AM”。
我希望它显示为“31.12.2008”。
我该怎么做? 我见过其他 stackoverflow 问题提到 StringFormat 但他们以我无法工作的方式使用多重绑定。
这是我想要的语法(这是伪代码),只需在需要的地方指定 StringFormat,这是否可能?
<StackPanel>
<ListView ItemsSource="{Binding Orders}">
<ListView.View>
<GridView>
<GridViewColumn
Header="Order ID"
DisplayMemberBinding="{Binding Path=OrderID}"
StringFormat="{}{1:dd.MM.yyyy}"/>
<GridViewColumn
Header="Order Date"
DisplayMemberBinding="{Binding Path=OrderDate}"/>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
I'm deep in a XAML stack of elements binding to orders.
The order date displays as e.g. "12/31/2008 12:00:00 AM".
I want it to display as e.g. "31.12.2008".
How can I do this? I have seen other stackoverflow questions mention StringFormat but they use multibinding in ways that I can't get to work.
Here is the kind of syntax I would like (this is pseudocode), simply specifying StringFormat where you need it, is this possible somehow?
<StackPanel>
<ListView ItemsSource="{Binding Orders}">
<ListView.View>
<GridView>
<GridViewColumn
Header="Order ID"
DisplayMemberBinding="{Binding Path=OrderID}"
StringFormat="{}{1:dd.MM.yyyy}"/>
<GridViewColumn
Header="Order Date"
DisplayMemberBinding="{Binding Path=OrderDate}"/>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我还没有测试过,但我认为这应该有效:
I haven't tested it, but I think this should work:
一般来说,您可以查找关联的 *StringFormat 依赖属性。 例如,所有 ContentControl< /a> 实现(例如标签和工具提示)具有 ContentStringFormat 依赖属性:
在您的情况下,而 GridViewColumn 具有 HeaderStringFormat 依赖属性 与
Header
一起使用,DisplayMemberBinding 没有类似的东西
因此您将需要 .NET 3.5 SP1(通过 Visual Studio 2008 SP1) 或更好的版本以使用新的 BindingBase.StringFormat 属性:还有更多示例在博客文章尝试 Binding.StringFormat。
In general, you can look for an associated
*StringFormat
dependency property. For example, all ContentControl implementations (such as Label and Tooltip) have the ContentStringFormat dependency property:In your case, while the GridViewColumn has the HeaderStringFormat dependency property to go along with
Header
, there is no analog for theDisplayMemberBinding
and so you will need .NET 3.5 SP1 (get it with Visual Studio 2008 SP1) or better to use the new BindingBase.StringFormat Property:There are lots more examples at the blog post Trying out Binding.StringFormat.
XAML
C#
XAML
C#
如果您想本地化日期格式,可以将其包含在 .resx 文件中。 您必须按照本指南设置您的应用程序以进行本地化:https: //developer.xamarin.com/guides/xamarin-forms/advanced/localization/。
resx 条目:
在您的内容页面中,您可以包含 resx 文件的位置
,然后您可以在绑定中使用它,如下所示:
If you wanted to localize the date format, you can include it in your .resx file. You will have to set up your app for localization by following this guide: https://developer.xamarin.com/guides/xamarin-forms/advanced/localization/.
The resx entry:
In your content page, you then include the location of the resx file
And then you can use it in your binding like so: