You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF NumberTextBox仅允许输入两位小数问题求助

解决NumberTextBox限制两位小数的问题

嘿,我来帮你搞定这个NumberTextBox的小数位数限制问题!你遇到的StringFormat不生效的情况很常见——因为StringFormat只是用来格式化显示值,它不会约束用户的输入,也不会自动截断多余的小数位。下面给你几个靠谱的解决方案:

方案1:利用NumberTextBox内置属性(最推荐)

很多自定义的NumberTextBox控件都会提供专门控制小数位数的属性,比如DecimalPlaces、MaxDecimalPlaces之类的。你可以直接在XAML里添加这个属性:

<cc:NumberTextBox Margin="5,0,5,0" 
                  HorizontalAlignment="Stretch" 
                  IsEnabled="{Binding IsEditable}" 
                  Style="{StaticResource TextBox_Default}" 
                  TabIndex="25" 
                  IsDecimalAllowed="True"
                  DecimalPlaces="2" <!-- 新增这个属性 -->
                  Text="{Binding ProductQuantity, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" />

这样控件会直接限制用户最多输入两位小数,输入超过的部分会被自动截断,完全符合你的需求。

方案2:在ViewModel的属性Setter中处理

如果你的NumberTextBox没有内置的小数位数属性,那可以在ViewModel的ProductQuantity属性里手动截断多余的小数位。比如你要保留两位小数且截断(不是四舍五入),可以这么写:

private decimal _productQuantity;
public decimal ProductQuantity
{
    get => _productQuantity;
    set
    {
        // 截断到两位小数:先乘100取整,再除以100
        var truncatedValue = Math.Truncate(value * 100) / 100;
        // 如果需要四舍五入的话,换成下面这行:
        // var roundedValue = Math.Round(value, 2, MidpointRounding.AwayFromZero);
        
        if (_productQuantity != truncatedValue)
        {
            _productQuantity = truncatedValue;
            OnPropertyChanged(); // 记得实现INotifyPropertyChanged接口
        }
    }
}

这样不管用户输入多少位小数,最终绑定的属性值都会被截断到两位,显示的时候自然就是你想要的结果。

方案3:自定义ValidationRule验证并修正值

你还可以通过自定义验证规则,在输入完成后自动修正值并验证:

首先创建一个ValidationRule类:

public class MaxDecimalPlacesRule : ValidationRule
{
    public int MaxDecimalPlaces { get; set; } = 2;

    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        if (decimal.TryParse(value?.ToString(), out var inputValue))
        {
            // 计算截断后的值
            var multiplier = (decimal)Math.Pow(10, MaxDecimalPlaces);
            var correctedValue = Math.Truncate(inputValue * multiplier) / multiplier;

            // 如果输入值和修正后的值不一致,更新绑定源
            if (inputValue != correctedValue)
            {
                var bindingExpr = ValidationStepResult as BindingExpression;
                bindingExpr?.UpdateSource(correctedValue);
            }
            return ValidationResult.ValidResult;
        }
        return new ValidationResult(false, "请输入有效的数字");
    }
}

然后在XAML里绑定的时候添加这个验证规则:

<cc:NumberTextBox Margin="5,0,5,0" 
                  HorizontalAlignment="Stretch" 
                  IsEnabled="{Binding IsEditable}" 
                  Style="{StaticResource TextBox_Default}" 
                  TabIndex="25" 
                  IsDecimalAllowed="True">
    <cc:NumberTextBox.Text>
        <Binding Path="ProductQuantity" 
                 ValidatesOnDataErrors="True" 
                 UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <local:MaxDecimalPlacesRule MaxDecimalPlaces="2" ValidationStep="UpdatedValue"/>
            </Binding.ValidationRules>
        </Binding>
    </cc:NumberTextBox.Text>
</cc:NumberTextBox>

这个方案会在用户输入后自动修正值,同时保持验证逻辑的独立性。


内容的提问来源于stack exchange,提问作者sara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:34:02