【问题标题】:XAML databinding and dynamic string formatXAML 数据绑定和动态字符串格式
【发布时间】:2012-11-20 03:28:08
【问题描述】:

只是在数据绑定时格式化显示的字符串有些问题。

假设我有一个属性 Size:

    /// <summary>
    /// Size information
    /// </summary>
    private long _size;
    public long Size
    {
        get
        {
            return _size;
        }
        set
        {
            if (value != _size)
            {
                _size = value;
                NotifyPropertyChanged("Size");
            }
        }
    }

这个大小是一个整数,代表字节数。我想根据整数的大小显示一个表示大小的值。例如:

Size = 1 byte
Size = 1 kilobyte
Size = 100 megabytes

这是我的 TextBlock 的 XAML:

<TextBlock Text="{Binding Size, StringFormat='Size: {0}'}" TextWrapping="Wrap" Margin="12,110,0,0" Style="{StaticResource PhoneTextSubtleStyle}" Visibility="{Binding Visible}" FontSize="14" Height="20" VerticalAlignment="Top" HorizontalAlignment="Left" Width="200"/>

到目前为止,它只显示“大小:50”,意思是 50 个字节,但我希望它显示“大小:50 字节/千字节/兆字节”(以合适的为准),否则我会得到“大小:50000000000000 "以及类似的大量数字。

我将如何“动态”更改字符串格式?

请记住,文本块被封装在由 ObservableCollection 界定的 LongListSelector 中,因此如果您知道我的意思,那么简单地获取文本块并更改文本将不起作用,因为会有大量使用文本块格式的对象。

谢谢。

【问题讨论】:

    标签: c# xaml binding string-formatting


    【解决方案1】:

    就我而言,我使用了一些技巧。我将视图绑定到一个 viewModel,其中包含一个包含格式化值的附加字符串属性。例如,像这样:

    //using short scale: http://en.wikipedia.org/wiki/Long_and_short_scales#Comparison
    const decimal HundredThousand = 100 * 1000;
    const decimal Million = HundredThousand * 10;
    const decimal Billion = Million * 1000; //short scale
    const decimal Trillion = Billion * 1000; //short scale
    
    const string NumberFormatKilo = "{0:##,#,.## K;- ##,#,.## K;0}";
    const string NumberFormatMillion = "{0:##,#,,.## M;- ##,#,,.## M;0}";
    const string NumberFormatBillion = "{0:##,#,,,.## B;- ##,#,,,.## B;0}";
    const string NumberFormatTrillion = "{0:##,#,,,,.## T;- ##,#,,,,.## T;0}";
    
    public decimal Size 
    {
        get; set;
    }
    
    public string SizeFormatted 
    {
        get 
        {
            var format = GetUpdatedNumberFormat(Size);
            return string.Format(format, Size);
        }
    }
    
    private static string GetUpdatedNumberFormat(decimal value)
    {
        string format = NumberFormat;
        if (Math.Abs(value) >= Constants.Trillion)
            format = NumberFormatTrillion;
        else if (Math.Abs(value) >= Constants.Billion)
            format = NumberFormatBillion;
        else if (Math.Abs(value) >= Constants.Million)
            format = NumberFormatMillion;
        else if (Math.Abs(value) >= Constants.HundredThousand)
            format = NumberFormatKilo;
        return format;
    }
    

    现在,将视图绑定到这个 SizeFormatted 属性:

    <TextBlock Text="{Binding SizeFormatted}" ...
    

    【讨论】:

    • 正是我想要的。我有一个类似的想法,即 Size 的属性将返回格式正确的字符串,但是由于返回类型和将我的数据转换为正确的类型而出现了一些问题。谢谢!
    • 你应该使用转换器。
    • 当然 - 这也行。虽然它可能会有点令人费解。
    猜你喜欢
    • 1970-01-01
    • 2018-02-14
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 2013-01-31
    • 1970-01-01
    • 2016-10-05
    相关资源
    最近更新 更多