【问题标题】:Convert Selected Combobox item to a decimal and store it in the database将 Selected Combobox 项目转换为小数并将其存储在数据库中
【发布时间】:2021-05-03 05:55:32
【问题描述】:

我的问题: 我有一个组合框,其值为:11,251,351,451 和 551。 我在 WPF 中使用 MVVM 模式,并且我想使用转换器,将所选值转换为小数并使用绑定将其保存到数据库中。

当我尝试转换值时,我的 Converter 中的 ConvertBack 方法出现以下异常。

这是我的转换器的代码:

        {
            try
            {
                string str_value = value.ToString();
                decimal decimal_myvalue = decimal.Parse(str_value);
                return decimal_myvalue;

            }
            catch (Exception e)
            {

                var a = e.Message;
            }
            return 0;
            
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

这是我的组合框的 xaml 代码:

<ComboBox SelectedValue="{Binding Transaction.PoisonSeeds, Converter={StaticResource PoisonSeedValueConverter}}" ...>
    <ComboBoxItem Content="11"/>
    <ComboBoxItem Content="251"/>
    <ComboBoxItem Content="351"/>
    <ComboBoxItem Content="451"/>
    <ComboBoxItem Content="551"/>
</ComboBox>

有什么值得注意的我做错了吗?

另一种解决方案是什么?

【问题讨论】:

  • “我在 WPF 中使用 MVVM 模式”——它在哪里?我没看到它周围
  • 设置 SelectedValuePath="Content"(并阅读 ItemsControl 文档以了解 SelectedValue 的工作原理)。除此之外,将 ItemsSource 绑定到小数集合,然后绑定 SelectedItem 而不是 SelectedValue 可能更简单(并且更多 MVVM)。
  • 我没有调用 Convertback 方法。它甚至没有进入转换方法
  • 试试,谢谢 clemens

标签: c# wpf mvvm


【解决方案1】:

很明显,你在 convert back 中抛出异常......但除此之外,还有很多问题。让我解释一下……

在转换器的ConvertBack 方法中放置断点就足以看到要转换的值是ComboBoxItem - 这不是您所期望的。

或者,查看输出窗口(如果您使用 Visual Studio),您还可以在其中获得一些信息,了解应用“内部”发生的情况:

System.Windows.Data Error: 23 : Cannot convert 'System.Windows.Controls.ComboBoxItem: 11' from type 'ComboBoxItem' to type 'System.Decimal' for 'en-US' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: DecimalConverter cannot convert from System.Windows.Controls.ComboBoxItem.
   at System.ComponentModel.TypeConverter.GetConvertFromException(Object value)
   at System.ComponentModel.TypeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)'

因此,您应该绑定到视图模型中的集合,而不是定义组合项,这将保留小数 - 然后您只需绑定到选定的项目,无需转换任何内容。

所以你必须选择:

  1. 脏 - 在您的转换器中,修改代码,使其处理 ComboboxItem 而不是纯字符串(我可以从您的代码中假设)。

  2. 清洁 MVVM 方式(@Clemens 建议): 在您的视图模型项目源上定义

public ObservableCollection<decimal> CbxItems { get; } = new ObservableCollection<decimal>();

并在 XAML 中绑定到它:

<Combobox ItemsSource="{Binding CbxItems}" SelectedItem="{Binding MyDecimal}" />

【讨论】:

    【解决方案2】:

    您应该使用ConvertBack 方法而不是Convert,因为您正在写入源代码

    Convert 中,您应该从十进制转为字符串。

    'ConvertBack' 你应该从字符串转换为十进制

    正如@Clements 指出的那样:添加 SelectedValuePath="Content"

    【讨论】:

      猜你喜欢
      • 2012-07-29
      • 1970-01-01
      • 1970-01-01
      • 2020-08-27
      • 2012-03-18
      • 2016-02-07
      • 2020-11-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多