我有一个Textbox,它绑定到viewmodel中的属性。我也使用转换器将十进制值转换为货币值。例如,如果我输入255,则文本值应显示为$ 255。但它似乎没有起作用。
<TextBox Margin="479,69,0,0"
Height="24"
Text="{bindingDecorators:CurrencyBinding FleetAggregate, Mode=TwoWay, Converter={StaticResource DecimalToCurrencyConverter}}" />
虚拟机资源
public decimal? FleetAggregate
{
get { return FleetRatesRow.HasStandardAggregate ? (decimal?)FleetRatesRow.fleet_pa_aggregate : (decimal?)null; }
set
{
if (!value.HasValue)
{
FleetRatesRow.Setfleet_pa_aggregateNull();
OnPropertyChanged();
return;
}
FleetRatesRow.fleet_pa_aggregate = value.Value;
OnPropertyChanged();
}
}
转换器
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
try
{
var currencyFormatArg = parameter == null ? new CurrencyFormatArg() : (CurrencyFormatArg)parameter;
if (value == null)
{
return currencyFormatArg.AllowNull ? string.Empty : DependencyProperty.UnsetValue;
}
var currencyValue = (decimal)value;
string format = currencyValue < 0
? '-' + currencyFormatArg.Format
: currencyFormatArg.Format;
string codePrefix = currencyFormatArg.ShowCode ? currencyFormatArg.CurrencyCode + " " : string.Empty;
return
codePrefix +
string.Format(format, currencyFormatArg.CurrencySymbol, Math.Abs(currencyValue));
}
catch (Exception)
{
return DependencyProperty.UnsetValue;
}
}
答案 0 :(得分:1)
您可以使用StringFormat
而不是使用转换器。它还不清楚bindingDecorators:CurrencyBinding
是什么。该物业本身也隐藏了很多复杂性。到目前为止还没有mcve。
无论如何decimal?
应该像魅力一样:
<强> XAML 强>
<TextBox Text="{Binding Path=FleetAggregate, StringFormat=${0:0.00}}"/>
代码背后
public class DummyViewModel
{
private decimal? _fleetAggregate;
public decimal? FleetAggregate
{
get
{
return _fleetAggregate;
}
set
{
_fleetAggregate = value;
//OnPropertyChanged();
}
}
}