[英]Binding double value to textbox
I have an ObservableCollection
filling a Datagrid DGCommands
, and some textboxes' Text
attribute defined like the following : 我有一个
ObservableCollection
填充Datagrid DGCommands
,一些文本框的Text
属性定义如下:
Text="{Binding SelectedItem.Costs,
Mode=TwoWay,
ElementName=DGCommands,
UpdateSourceTrigger=PropertyChanged}"
Alright, now when i parse a double value from one of the textboxes, for example : 好吧,现在当我从其中一个文本框中解析一个double值时,例如:
bool costs_valid = double.TryParse(txtboxCosts.Text, out _costs) // 1,8 i get 18;
I tried to solve the issue with this method but i'm not sure if it's the best possible solution : 我尝试用这种方法解决问题,但我不确定它是否是最好的解决方案:
public static double DoubleParseCostum(string val)
{
double res = 0;
bool res_valid = double.TryParse(val, out res);
if (val.ToString().Contains(_DECIMAL_)) return res;
else
{
if ("." != _DECIMAL_ && val.Contains(".")) return double.Parse(val.Replace(".", _DECIMAL_));
if ("," != _DECIMAL_ && val.Contains(",")) return double.Parse(val.Replace(",", _DECIMAL_));
}
return res;
}
Where 哪里
_DECIMAL_ = Convert.ToChar(System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator).ToString();
EDIT : 编辑:
Changed the title from " Parsing string to double gives Int " To the current one because the real problem was in the binding and not the parsing, cuz when i want to change 1.8 to 1,8 i get 18 as result on lost focus. 将标题从“ 解析字符串更改为双给Int ”更改为当前的标题,因为真正的问题在于绑定而不是解析,因为当我想要将1.8更改为1,8时我得到18作为失去焦点的结果。
试试这个: double.Parse("1.8", CultureInfo.InvariantCulture)
In this example I picked a culture (Austria) that uses the comma as a decimal separator to show how to do it. 在这个例子中,我选择了一个文化(奥地利),它使用逗号作为小数分隔符来显示如何操作。 You can set the culture to anything you want, of course, including the current UI culture.
您可以将文化设置为您想要的任何内容,当然,包括当前的UI文化。
var testCulture = CultureInfo.CreateSpecificCulture("de-AT");
var text = "1,8";
double result;
var ok = double.TryParse(text, NumberStyles.Float, testCulture, out result);
Console.WriteLine("And the result is: {0:000.0000}", result);
Output: 输出:
And the result is: 001.8000
This is a long-shot... I'm wondering whether the underlying problem that you're actually trying to solve (without realising it) is that all WPF applications default to using the "en-US" locale. 这是一个长镜头...我想知道你实际上试图解决的基本问题(没有意识到)是否所有WPF应用程序默认使用“en-US”语言环境。 You can solve this on startup of the application by doing:
您可以通过以下操作在启动应用程序时解决此问题:
FrameworkElement
.LanguageProperty
.OverrideMetadata(
typeof(FrameworkElement),
new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentUICulture.IetfLanguageTag)));
I finally found a solution, just like @Richardissimo said, WPF uses the 'en-US' culture by default and comma (,) is then treated as a thousand separator and not as a decimal sign. 我终于找到了一个解决方案,就像@Richardissimo所说的那样,WPF默认使用'en-US'文化,然后逗号(,)被视为千分隔符而不是小数符号。 so i set the ConverterCulture property to use the system's default culture :
所以我设置ConverterCulture属性以使用系统的默认文化:
<TextBox Name="txtboxCosts" Style="{StaticResource txtboxDefault2}"
xmlns:gl="clr-namespace:System.Globalization;assembly=mscorlib"
Text="{Binding SelectedItem.proCosts,
Mode=TwoWay,
ElementName=DGCommands,
StringFormat={}{0:N2},
UpdateSourceTrigger=PropertyChanged,
ConverterCulture={x:Static gl:CultureInfo.CurrentCulture}}"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.