2017-01-04 5 views
0

私はTextModelを持ち、viewmodelのプロパティにバインドされています。また、コンバータを使用して10進値を通貨値に変換しています。たとえば、255を入力すると、テキスト値は$ 255と表示されます。しかし、それは動作していないようです。OnPropertyChangedはNULL値の10進数型wpfで動作しません

<TextBox Margin="479,69,0,0" 
        Height="24" 
        Text="{bindingDecorators:CurrencyBinding FleetAggregate, Mode=TwoWay, Converter={StaticResource DecimalToCurrencyConverter}}" /> 

VMプロパティ

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

あなたがコンバータを使用する代わりにStringFormatを使用することができ

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

答えて

1

コンバータ。 bindingDecorators:CurrencyBindingが何であるかも明確ではありません。プロパティー自体も多くの複雑さを隠しています。これははるかにないmcveです。とにかく

decimal?は魅力のように動作するはずです:

public class DummyViewModel 
{ 
    private decimal? _fleetAggregate; 
    public decimal? FleetAggregate 
    { 
     get 
     { 
      return _fleetAggregate; 
     } 
     set 
     { 
      _fleetAggregate = value; 
      //OnPropertyChanged(); 
     } 
    } 


} 
の後ろ

XAML

<TextBox Text="{Binding Path=FleetAggregate, StringFormat=${0:0.00}}"/> 

コードを

関連する問題