Silverlight XAML 绑定——十进制到双精度

发布于 2024-08-08 23:28:40 字数 195 浏览 2 评论 0原文

我在 DataGridView 中有一些列,其 Binding 属性设置为如下所示:

Binding="{Binding NetPrice}"

问题是此 NetPrice 字段是 Decimal 类型,我想将其转换为 DataGrid 内的 Double 类型。

有什么方法可以做到这一点吗?

I've got some Columns in a DataGridView that have their Binding property set to something like the following:

Binding="{Binding NetPrice}"

The problem is that this NetPrice field is a Decimal type and I would like to convert it to Double inside the DataGrid.

Is there some way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

旧城空念 2024-08-15 23:28:40

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

很多资源 用于创建转换器。它们也很容易在 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}"

I would create a Converter. A converter takes one variable and "converts" it to another.

There are a lot of resources for creating converters. They are also easy to implement in c# and use in xaml.

Your converter could look similar to this:

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"); 
    }   
}

Once you've created your converter, you will need to tell your xaml file to include it like this:

in your namespaces (at the very top of your xaml), include it like so:

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

Then declare a static resource, like so:

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

Then add it to your binding like this:

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