简体   繁体   English

绑定ComboBox SelectedItem为double

[英]Binding ComboBox SelectedItem to double

My ComboBox's ItemsSource is binding to a List<double> options and the SelectedItem is binding to a property double SelectedOption . 我的ComboBox的ItemsSource绑定到List<double> optionsSelectedItem绑定到属性double SelectedOption

If options contains the values 0.0060F, 0.0075F, 0.0100F and SelectedItem = 0.0060F , the ComboBox does should, but does not, reflect this. 如果options包含值0.0060F, 0.0075F, 0.0100FSelectedItem = 0.0060F ,则ComboBox应该但不会反映这一点。 Instead, selecting the ComboBox shows the options list items with their strange decimal representations. 相反,选择ComboBox会显示带有奇怪的十进制表示的options列表项。 Is it even possible to do data binding on variables of type double with their unpredictable representation? 甚至可以使用不可预测的表示形式对double类型的变量进行数据绑定吗?

Code

ViewModel: 视图模型:

//Properties
public List<double> Options{get;set;}
public double SelectedOption{get;set;}

//Constructor
public ViewModel()
{
    Options =  new List<double>();
    Options.Add(0.0060F);
    Options.Add(0.0075F);
    Options.Add(0.0100F);

    SelectedOption = 0.0060F;
}

Binding: 捆绑:

<ComboBox ItemsSource="{Binding Path=Options}" 
    SelectedItem="{Binding Path=SelectedOption, Mode=TwoWay}" />

The options that are shown in the ComboBox are: ComboBox中显示的选项是:

  • 0.00600000005215406 0.00600000005215406
  • 0.00749999983236194 0.00749999983236194
  • 0.00999999977648258 0.00999999977648258

Maybe you should give a read at what floating points are: http://csharpindepth.com/Articles/General/FloatingPoint.aspx 也许你应该读一下浮点数: http//csharpindepth.com/Articles/General/FloatingPoint.aspx

Those values are used to give a as close as possible to reality representation of numbers. 这些值用于尽可能接近数字的现实表示。 If you want exact values, use System.Decimal 如果需要精确值,请使用System.Decimal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM