【发布时间】:2017-01-04 15:40:58
【问题描述】:
我有一个文本框,它绑定到视图模型中的一个属性。我还使用转换器将十进制值转换为货币值。例如,如果我输入 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;
}
}
【问题讨论】:
标签: c# wpf data-binding