【问题标题】:WPF MVVM DataGridColumn with conditional StringFormat具有条件 StringFormat 的 WPF MVVM DataGridColumn
【发布时间】:2019-05-15 23:44:28
【问题描述】:

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

【问题讨论】:

    标签: c# wpf mvvm wpfdatagrid


    【解决方案1】:

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

    【讨论】:

    • 谢谢 :) 我的代码正在使用“NotifyPropertyChanged”,所以我像上面一样将“OnPropertyChanged”更改为“NotifyPropertyChanged”(更新了我的代码),但它不起作用。有什么问题吗?
    • 我很快测试了它,它按预期工作。哪一部分不行?您是否将 xaml 中的绑定更改为绑定到 FormattedAmount 而不是 Amount? Binding="{Binding Amount, 应改为 Binding="{Binding FormattedAmount,
    • 我错过了那部分。你救了我的一天!谢谢你:)
    猜你喜欢
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 2014-10-19
    • 2014-10-04
    • 2012-04-02
    相关资源
    最近更新 更多