如何在WPF中处理百分比输入和显示

本文关键字:百分比 输入 显示 处理 WPF | 更新日期: 2023-09-27 18:15:59

我一直在寻找一种处理百分比的好方法。在我们的应用程序中,我们需要显示和编辑百分比值,使用不同的十进制数字,即,当用户当前不关注文本框时,它应该显示12.50%,但在编辑值时,它应该显示为0.12501015512312311。更糟糕的是,用户应该能够选择在运行时显示的小数数量。此外,应该允许用户输入12.5%,这将转换为0.125。我知道我可以用转换器做到这一点,但是我在将所有这些要求组合成一个体面的解决方案时遇到了麻烦。

这是我迄今为止最好的尝试:

public class PercentConverter : IValueConverter
{
    private static int numberOfDecimals = CultureInfo.CurrentCulture.NumberFormat.PercentDecimalDigits;
    public static int NumberOfDecimals
    {
        get { return numberOfDecimals; }
        set
        {
            if(value != numberOfDecimals)
            {
                numberOfDecimals = value;
            }
        }
    }
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        decimal pct = System.Convert.ToDecimal(value);
        CultureInfo ci = new CultureInfo(CultureInfo.CurrentCulture.Name, false);
        ci.NumberFormat.PercentDecimalDigits = NumberOfDecimals;
        return String.Format(ci, "{0:P}", pct);
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string percentString = value as string;
        string doubleString = percentString.TrimEnd(culture.NumberFormat.PercentSymbol.ToCharArray());
        double percent = Double.Parse(doubleString, NumberStyles.Any);
        if (IsExplicitPercentage(percentString, culture))
        {
            percent /= 100;
        }
        return percent;
    }
    private bool IsExplicitPercentage(string percentString, CultureInfo culture)
    {
        return percentString.Contains(culture.NumberFormat.PercentSymbol);
    }
}

,然后在xaml

<DataGrid.Columns>
            <DataGridTextColumn Header="Beskrivelse" Binding="{Binding Description}" />
            <DataGridTemplateColumn Header="Share">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox HorizontalContentAlignment="Right" HorizontalAlignment="Stretch">
                            <TextBox.Style>
                                <Style TargetType="{x:Type TextBox}">
                                    <!-- Other Style Setters -->
                                    <Setter Property="Text" Value="{Binding Share, Converter={StaticResource pctConverter}}" />
                                    <Style.Triggers>
                                        <Trigger Property="IsKeyboardFocused" Value="True">
                                            <Setter Property="Text" Value="{Binding Share}" />
                                        </Trigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBox.Style>
                        </TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>

但是这不会正确地绑定到数据上下文,当我改变文本框中的值时,它不会更新它所绑定的模型上的属性。

请注意,我对WPF很陌生,所以我可能会错过一些基本的东西。

如何在WPF中处理百分比输入和显示

您需要修改Text属性的绑定:

<Setter Property="Text" Value="{Binding Share, Converter={StaticResource pctConverter}, UpdateSourceTrigger=PropertyChanged}" />

这将导致DataContext对象属性中的值在键入每个字符时发生变化。默认情况下,属性在字段失去焦点后更改。