2016-07-30 3 views
0

私はWPFでテキストフィールドを持っていますが、私はユーザー入力を検証したいと思っています。WPF Textfield Validation

数字だけを許可する必要があります(0-9) 小数点以下1桁のみ 複数の '。'を許可しません。ほとんどのソリューションができるように思えた、 最小数のエントリは、私がこれまでに正規表現

new Regex(@"[^0-9.]+") 

を使用していた 最大999.9

ベストソリューションでなければなりませんが、これは明らかに小数点以下の桁または数字の数に制限はありません小数点の最小値も最大値もありません

私は正しい方向に1つしかポイントできませんか?

おかげ

答えて

0

クリーナーソリューションは、あなたの自動桁と小数点のルールを与え、あなたの最小値と最大値の[Range]属性を追加しますdouble(またはdecimal)値にテキストボックスをバインドするかもしれません。

[Range(0, 999.9), "Error message"] 
public double myValue { get; set; } 

または、あなたが実際には、最初の場所で入力されている不正な入力を防止するためのテキストボックスのOnKeyDownイベントハンドラを実装し、番号の入力を変換しようとする場合。失敗した場合は入力を拒否するか、成功したにもかかわらず番号が範囲外にある場合は入力を拒否します。これは理想的な解決策ではありません。

0

私は、一般的に、このアプローチを単純性と柔軟性のために使用しています。 Regexと普通の条件論理の謙虚な組み合わせ。この同じパターンは、ほぼすべての形式のテキスト入力検証に適用できます。

のViewModel

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.ComponentModel; 
using System.Text.RegularExpressions; 

namespace WpfApplication7 
{ 
    public class ViewModel : INotifyPropertyChanged 
    { 
     Regex _inputRegex; 
     public ViewModel() 
     { 
      _inputRegex = new Regex(@"^([0-9])+(([.])?([0-9])+)?$"); 
     } 
     private string _input = "0"; 
     public string Input 
     { 
      get 
      { 
       return _input; 
      } 
      set 
      { 
       _input = value; 
       RaisePropertyChanged("Input"); 
       RaisePropertyChanged("InputValid"); 
      } 
     } 
     public bool InputValid { 
      get 
      { 
       if(_inputRegex.IsMatch(_input)) 
       { 
        //If regex pattern is satisfied, this value is safe 
        double value = Convert.ToDouble(_input); 

        //so just apply conditional logic here 
        return value >= 0 && value <= 999.9; 
       } 
       else 
       { 
        return false; 
       } 
      }   
     } 
     public event PropertyChangedEventHandler PropertyChanged; 
     public void RaisePropertyChanged(string property) 
     { 
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property)); 
     } 
    } 
} 

ビュー

<Window x:Class="WpfApplication7.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:WpfApplication7" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.DataContext> 
     <local:ViewModel /> 
    </Window.DataContext> 
    <Window.Resources> 
     <Style TargetType="FrameworkElement" > 
      <Setter Property="HorizontalAlignment" Value="Center" /> 
      <Setter Property="VerticalAlignment" Value="Center" /> 
     </Style> 
     <Style TargetType="TextBox"> 
      <Setter Property="Width" Value="100" /> 
      <Setter Property="Height" Value="24" /> 
      <Setter Property="VerticalContentAlignment" Value="Center" /> 
      <Setter Property="Margin" Value="10" /> 
     </Style> 
     <Style TargetType="TextBlock" x:Key="default_textblock"> 
      <Setter Property="Height" Value="18" /> 
      <Setter Property="Margin" Value="10" /> 
     </Style> 
     <Style TargetType="TextBlock" x:Key="error_textblock" BasedOn="{StaticResource default_textblock}"> 
      <Setter Property="Foreground" Value="Red" /> 
     </Style> 
    </Window.Resources> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="60" /> 
      <RowDefinition Height="60" /> 
     </Grid.RowDefinitions> 
     <StackPanel Orientation="Horizontal" Grid.Row="0"> 
      <TextBlock Text="Enter input: " Style="{StaticResource default_textblock}" /> 
      <TextBox Text="{Binding Input, UpdateSourceTrigger=PropertyChanged}" /> 
     </StackPanel> 
     <TextBlock Grid.Row="1" Text="Input not valid. Please enter a number between 0 and 999.9" > 
      <TextBlock.Style> 
       <Style TargetType="TextBlock" BasedOn="{StaticResource error_textblock}"> 
        <Setter Property="Visibility" Value="Hidden" /> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding InputValid}" Value="False"> 
          <Setter Property="Visibility" Value="Visible" /> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </TextBlock.Style> 
     </TextBlock> 
    </Grid> 
</Window>