我会创建一个转换器。转换器接受一个变量并将其“转换”为另一个变量。
lot 和 resources 中有一个用于创建转换器。它们也很容易在 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}"