WPF 文本框绑定百分比
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12138603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF TextBox Binding Percentage
提问by Florian
I have to bind a TextBox to a % value. So I set the StringFormat Property on the Binding like this:
我必须将 TextBox 绑定到 % 值。所以我在 Binding 上设置 StringFormat 属性,如下所示:
<TextBox Text="{Binding Path=BewertungsFaktore.Gewinn, StringFormat=P2, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource TextBoxStyle}" HorizontalAlignment="Left"/>
That seems to work quite fine. But if I edit the value there are issues. As example if I enter a value of 1 the textbox should format this in 1%. But the main problem is it formats into 100.00%. Another question is if I am using a German Sys do I have to enter ',' instaed of a '.'?
这似乎工作得很好。但是,如果我编辑该值,则会出现问题。例如,如果我输入值 1,则文本框应将其格式化为 1%。但主要问题是它格式化为 100.00%。另一个问题是,如果我使用的是德语系统,我是否必须输入“,”而不是“.”?
回答by Brian Hinchey
If you just use a string format like {0:F2}%then you will have to put up with database values like 4.23 to represent 4.23% which is unacceptable to me (until they introduce a "percentage" data type into SQL Server).
如果你只是使用像那样的字符串格式,{0:F2}%你将不得不忍受像 4.23 这样的数据库值来表示 4.23%,这对我来说是不可接受的(直到他们将“百分比”数据类型引入 SQL Server)。
I created the following value converter to map percentage values in the TextBox like 4.2367% into database values like 0.042367:
我创建了以下值转换器来将 TextBox 中的百分比值(如 4.2367%)映射到数据库值(如 0.042367)中:
public class PercentageConverter : IValueConverter
{
//E.g. DB 0.042367 --> UI "4.24 %"
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var fraction = decimal.Parse(value.ToString());
return fraction.ToString("P2");
}
//E.g. UI "4.2367 %" --> DB 0.042367
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
//Trim any trailing percentage symbol that the user MAY have included
var valueWithoutPercentage = value.ToString().TrimEnd(' ', '%');
return decimal.Parse(valueWithoutPercentage)/100;
}
}
Note how the formatting string (in this case "P2") only limits how many decimal places are displayed, not how many decimal places are passed to the underlying binding source.
请注意格式化字符串(在本例中为“P2”)如何仅限制显示的小数位数,而不是传递给底层绑定源的小数位数。
回答by Clemens
The StringFormatproperty controls only the output string of the bound value. "P2" simply multiplies the value by 100 and formats it with two decimal digits and a trailing " %". On input, this format is ignored.
该StringFormat属性仅控制绑定值的输出字符串。“P2”只是将值乘以 100,并用两位十进制数字和尾随的“%”对其进行格式化。在输入时,此格式将被忽略。
If you also need to input percent value you would have to use a binding converter. Such a converter could also parse the input string in a culture-invariant way like this:
如果您还需要输入百分比值,则必须使用绑定转换器。这样的转换器还可以像这样以文化不变的方式解析输入字符串:
double fraction;
double percentage;
if (double.TryParse(value, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out percentage))
{
fraction = percentage / 100d;
}

