具有条件StringFormat的WPF MVVM DataGridColumn

时间:2019-05-15 23:44:29

标签: c# wpf mvvm wpfdatagrid

我的WPF MVVM应用程序的DataGridColumn'Amount'有两种模式取决于'Currency':

-美元金额:“ 1,000.12”(允许小数点后)
-日元金额:“ 5,000”(禁止小数点)

现在,它只有USD的StringFormat。要实现两个StringFormat模式,我应该使用<Style.Triggers>吗?

<DataGridTextColumn x:Name="PayAmt"
    Header="Amount" Binding="{Binding Amount, Mode=TwoWay,
    StringFormat={}{0:N2}, TargetNullValue='', UpdateSourceTrigger=LostFocus}" >
    <DataGridTextColumn.ElementStyle>
        <Style>
            <Setter Property="TextBlock.TextAlignment" Value="Right"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Amount}" Value="0"></DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

<DataGridTextColumn x:Name="Currency" Header="Currency" Binding="{Binding Currency, Mode=TwoWay}">
    <DataGridTextColumn.ElementStyle>
        <Style>
            <Setter Property="TextBlock.TextAlignment" Value="Left" />
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>


ViewModel:(更新后:在将以上绑定Amount更改为FormattedString后,此方法就起作用了!)

private Nullable<decimal> _amount { get; set; }
public Nullable<decimal> Amount {
    get { return _amount; }
    set
    {
        if (Equals(_amount, value)) return;
        _amount = value;
        NotifyPropertyChanged("Amount");
        NotifyPropertyChanged("FormattedAmount");
    }
}

private string _currency;

public string Currency
{
    get => _currency;
    set
    {
        if (_currency == value) return;
        _currency = value;
        NotifyPropertyChanged("Currency");
        NotifyPropertyChanged("FormattedAmount");
    }
}

public string FormattedAmount
{
    get
    {
        switch (Currency)
        {
            case "JPY":
                return Amount?.ToString("N0");
            default:
                return Amount?.ToString("N2");
        }
    }
    set
    {
        if (decimal.TryParse(value, out var amount))
            Amount = amount;
        else
            NotifyPropertyChanged("FormattedAmount");
    }
}

1 个答案:

答案 0 :(得分:0)

我建议在视图模型中这样做。您可以创建一个名为FormattedAmount的新属性并将其绑定到该属性。在getter中,仅根据货币格式化字符串。这是一个例子。

    private double _amount;

    public double Amount
    {
        get => _amount;
        set
        {
            if (_amount == value) return;
            _amount = value;
            OnPropertyChanged(nameof(Amount));
            OnPropertyChanged(nameof(FormattedAmount));
        }
    }

    private string _currency;

    public string Currency
    {
        get => _currency;
        set
        {
            if (_currency == value) return;
            _currency = value;
            OnPropertyChanged(nameof(Currency));
            OnPropertyChanged(nameof(FormattedAmount));
        }
    }

    public string FormattedAmount
    {
        get
        {
            switch (Currency)
            {
                case "JPY":
                    return Amount.ToString("N0");
                default:
                    return Amount.ToString("N2");
            }
        }
        set
        {
            if (double.TryParse(value, out var amount))
                Amount = amount;
            else
                OnPropertyChanged(nameof(FormattedAmount));
        }
    }

然后更改您的xaml以绑定到新属性

<DataGridTextColumn x:Name="PayAmt"
Header="Amount" Binding="{Binding FormattedAmount}" >
  <DataGridTextColumn.ElementStyle>
      <Style>
          <Setter Property="TextBlock.TextAlignment" Value="Right"/>
      </Style>
  </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>