【发布时间】: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