【问题标题】:Silverlight XAML Binding -- Decimal to DoubleSilverlight XAML 绑定——十进制到双精度
【发布时间】:2010-12-08 12:30:13
【问题描述】:

我在 DataGridView 中有一些列,它们的 Binding 属性设置如下:

Binding="{Binding NetPrice}"

问题是这个 NetPrice 字段是 Decimal 类型,我想在 DataGrid 中将它转换为 Double。

有没有办法做到这一点?

【问题讨论】:

  • 这不能按原样工作?

标签: silverlight data-binding xaml converter numeric-conversion


【解决方案1】:

我会创建一个转换器。转换器接受一个变量并将其“转换”为另一个变量。

lotresources 中有一个用于创建转换器。它们也很容易在 c# 中实现并在 xaml 中使用。

您的转换器可能如下所示:

public class DecimalToDoubleConverter : IValueConverter   
{   
    public object Convert( 
        object value,   
        Type targetType,   
        object parameter,   
        CultureInfo culture)   
    {   
        decimal visibility = (decimal)value;
        return (double)visibility;
    }   

    public object ConvertBack(   
        object value,   
        Type targetType,   
        object parameter,   
        CultureInfo culture)   
    {
        throw new NotImplementedException("I'm really not here"); 
    }   
}

一旦你创建了你的转换器,你需要告诉你的 xaml 文件来包含它,像这样:

在您的命名空间中(在您的 xaml 的最顶部),像这样包含它:

xmlns:converters="clr-namespace:ClassLibraryName;assembly=ClassLibraryName"

然后声明一个静态资源,像这样:

<Grid.Resources>
    <converters:DecimalToDoubleConverter x:Key="DecimalToDoubleConverter" />
</Grid.Resources>  

然后像这样将它添加到您的绑定中:

Binding ="{Binding Path=NetPrice, Converter={StaticResource DecimalToDoubleConverter}"

【讨论】:

  • 谢谢。我以前用过转换器,我只是想也许有一些更简单的数据类型的替代方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多