相关疑难解决方法(0)

十进制数正则表达式,其中十进制数字是可选的

我需要一个验证数字的正则表达式,但不需要小数点后的数字.即.

123
123.
123.4
Run Code Online (Sandbox Code Playgroud)

一切都会有效

123..
Run Code Online (Sandbox Code Playgroud)

会无效的

任何人将不胜感激!

regex

97
推荐指数
9
解决办法
22万
查看次数

WPF验证规则阻止文本框中的小数输入?

我有一个在XAML中定义的WPF文本框,如下所示:

<Window.Resources>        
    <Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<TextBox x:Name="upperLeftCornerLatitudeTextBox" Style="{StaticResource textBoxInError}">
    <TextBox.Text>
        <Binding Path="UpperLeftCornerLatitude" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <local:LatitudeValidationRule ValidationStep="RawProposedValue"/>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>
Run Code Online (Sandbox Code Playgroud)

如您所见,我的文本框绑定到我的业务对象上的一个名为UpperLeftCornerLatitude的十进制属性,如下所示:

private decimal _upperLeftCornerLongitude;
public decimal UpperLeftCornerLatitude
{
    get { return _upperLeftCornerLongitude; }
    set
    {
        if (_upperLeftCornerLongitude == value)
        {
            return;
        }

        _upperLeftCornerLongitude = value;
        OnPropertyChanged(new PropertyChangedEventArgs("UpperLeftCornerLatitude"));
    }
}
Run Code Online (Sandbox Code Playgroud)

我的用户将在此文本框中输入纬度值,为了验证该条目,我创建了一个如下所示的验证规则:

public class LatitudeValidationRule : ValidationRule
{
    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    { …
Run Code Online (Sandbox Code Playgroud)

c# validation wpf textbox

6
推荐指数
2
解决办法
8931
查看次数

标签 统计

c# ×1

regex ×1

textbox ×1

validation ×1

wpf ×1