【问题标题】:Add spaces between 9-digit binded number using StringFormat, xamarin.forms使用 StringFormat、xamarin.forms 在 9 位绑定数字之间添加空格
【发布时间】:2018-01-08 18:38:31
【问题描述】:

我有一个绑定号码,我想在其中添加空格。例如。目前它显示 123456789 ,我希望它显示 123 456 789。有人知道如何使用 stringFormat 吗?

我的 xaml

<Label Text="{Binding CardNumber}" TextColor="#1481BA" />

【问题讨论】:

  • 不完全确定但试试Text="{Binding CardNumber, StringFormat={0:### ### ###}"
  • 你是对的@Nkosi。将其发布为社区的答案 =)

标签: xaml xamarin xamarin.forms


【解决方案1】:

您可以使用以下格式StringFormat={0:### ### ###}

例如

<Label Text="{Binding CardNumber, StringFormat={0:### ### ###}" TextColor="#1481BA" />

【讨论】:

  • 这在我的 xamarin.forms xaml 中不起作用,而是在 .cs 类中格式化了我的号码
  • 您也可以考虑使用值转换器。
  • @JanineAlexander,这种格式应该适用于数字类型。 CardNumber 属性的类型是 string 吗?
【解决方案2】:

CardNumber 是一个字符串,因此 Nkosi 建议的格式不起作用。这是我的解决方案:

                    foreach(var i in Items)
                    {
                        string cardNum = i.CardNumber;
                        int cardInt = 3;

                        while(cardInt < cardNum.Length)
                        {
                            cardNum = cardNum.Insert(cardInt, " ");
                            cardInt += 4;
                        }
                        i.CardNumber = cardNum;

                    }

【讨论】:

    猜你喜欢
    • 2013-05-14
    • 2013-10-17
    • 2023-01-02
    • 2012-06-24
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多