如何在 XAML 元素中使用 StringFormat?

发布于 2024-07-15 06:47:38 字数 979 浏览 3 评论 0原文

我深入研究了绑定到订单的 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 技术交流群。

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

发布评论

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

评论(4

聽兲甴掵 2024-07-22 06:47:39

我还没有测试过,但我认为这应该有效:

<GridViewColumn
    Header="Order Date"
    DisplayMemberBinding=
        "{Binding Path=OrderDate, StringFormat='{}{0:dd.MM.yyyy}'}"/>

I haven't tested it, but I think this should work:

<GridViewColumn
    Header="Order Date"
    DisplayMemberBinding=
        "{Binding Path=OrderDate, StringFormat='{}{0:dd.MM.yyyy}'}"/>
神也荒唐 2024-07-22 06:47:39

一般来说,您可以查找关联的 *StringFormat 依赖属性。 例如,所有 ContentControl< /a> 实现(例如标签和工具提示)具有 ContentStringFormat 依赖属性

<Label
    Content="{Binding Path=DateAsked}"
    ContentStringFormat="{}{0:yyyy/MM/dd HH:mm:ss}" />

在您的情况下,而 GridViewColumn 具有 HeaderStringFormat 依赖属性Header 一起使用,DisplayMemberBinding 没有类似的东西 因此您将需要 .NET 3.5 SP1(通过 Visual Studio 2008 SP1) 或更好的版本以使用新的 BindingBase.StringFormat 属性

<GridViewColumn 
    Header="Order ID"
    DisplayMemberBinding="{Binding Path=OrderID, StringFormat='{}{0:dd.MM.yyyy}'}"
/>

还有更多示例在博客文章尝试 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:

<Label
    Content="{Binding Path=DateAsked}"
    ContentStringFormat="{}{0:yyyy/MM/dd HH:mm:ss}" />

In your case, while the GridViewColumn has the HeaderStringFormat dependency property to go along with Header, there is no analog for the DisplayMemberBinding 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:

<GridViewColumn 
    Header="Order ID"
    DisplayMemberBinding="{Binding Path=OrderID, StringFormat='{}{0:dd.MM.yyyy}'}"
/>

There are lots more examples at the blog post Trying out Binding.StringFormat.

风吹短裙飘 2024-07-22 06:47:39

XAML

<UserControl.Resources>
    <myNamespace:DateTimeConverter x:Key="DateTimeConverter" />
</UserControl.Resources>

<GridViewColumn 
DisplayMemberBinding=="{Binding Path=OrderDate, Converter={StaticResource DateTimeConverter}}" 
/>

C#

public class DateTimeConverter : IValueConverter
{
    public object Convert(object value,
                       Type targetType,
                       object parameter,
                       CultureInfo culture)
    {
        if (value != null)
        {
            return ((DateTime)value).ToString("dd.MM.yyyy");
        }
        else
        {
            return String.Empty;
        }
    }

    public object ConvertBack(object value,
                              Type targetType,
                              object parameter,
                              CultureInfo culture)
    {
        return DateTime.Parse(value.ToString());
    }
}

XAML

<UserControl.Resources>
    <myNamespace:DateTimeConverter x:Key="DateTimeConverter" />
</UserControl.Resources>

<GridViewColumn 
DisplayMemberBinding=="{Binding Path=OrderDate, Converter={StaticResource DateTimeConverter}}" 
/>

C#

public class DateTimeConverter : IValueConverter
{
    public object Convert(object value,
                       Type targetType,
                       object parameter,
                       CultureInfo culture)
    {
        if (value != null)
        {
            return ((DateTime)value).ToString("dd.MM.yyyy");
        }
        else
        {
            return String.Empty;
        }
    }

    public object ConvertBack(object value,
                              Type targetType,
                              object parameter,
                              CultureInfo culture)
    {
        return DateTime.Parse(value.ToString());
    }
}
長街聽風 2024-07-22 06:47:39

如果您想本地化日期格式,可以将其包含在 .resx 文件中。 您必须按照本指南设置您的应用程序以进行本地化:https: //developer.xamarin.com/guides/xamarin-forms/advanced/localization/

resx 条目:

<data name="DateFormat" xml:space="preserve">
    <value>{0:dddd d MMMM H:mm}</value>
</data>

在您的内容页面中,您可以包含 resx 文件的位置

xmlns:il8n="clr-namespace:MyProject.Localization;assembly=MyProject"

,然后您可以在绑定中使用它,如下所示:

<Label
    Text = "{Binding Time, StringFormat={il8n:Translate DateFormat}}"

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:

<data name="DateFormat" xml:space="preserve">
    <value>{0:dddd d MMMM H:mm}</value>
</data>

In your content page, you then include the location of the resx file

xmlns:il8n="clr-namespace:MyProject.Localization;assembly=MyProject"

And then you can use it in your binding like so:

<Label
    Text = "{Binding Time, StringFormat={il8n:Translate DateFormat}}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文