具有条件 StringFormat 的 WPF MVVM DataGridColumn

WPF MVVM DataGridColumn with conditional StringFormat

我的 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");
    }
}

我建议在视图模型中进行。您可以创建一个名为 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>