2013-09-01 6 views
8

私はこれまで簡単に想定していたことをしようとしています。別のコントロールの検証ルールで1つのコントロールの値を使用します。私のアプリケーションには、ユーザーが入力できるさまざまなパラメーターがあります。ここでの特定のパラメーターは、範囲の開始点と終了点を定義し、ユーザーはテキストボックスを使用して値を設定します。問題の他のコントロールの値を使用する検証ルール

2つのコントロールは、開始と終了のテキストボックスで、次の条件が検証で確認する必要があります:

  1. 開始値が
  2. 終了値がなければならないいくつかの任意の値以上でなければなりません私はすでにaccompl有する最初の2つの条件

未満またはいくつかの任意の値に等しい

  • スタート値は以下端と等しい値
  • なければならないことイシッド。 3番目は実装がはるかに難しいです。なぜなら、私はバリデータからテキストボックスの最終値にアクセスすることができないからです。可能であれば、5つの異なる範囲(それぞれに独自の開始テキストボックスと終了テキストボックスがあります)があります。私は検証しようとしており、それぞれの検証ルールを作成するよりも洗練されたソリューションが必要です。ここで

    は、関連するXAMLコードです:

    <Window x:Class="WpfApplication1.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:validators="clr-namespace:CustomValidators" 
        Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
        <Grid.RowDefinitions> 
         <RowDefinition/> 
         <RowDefinition/> 
        </Grid.RowDefinitions> 
    
        <TextBox Name="textboxStart" Grid.Row="0"> 
         <TextBox.Text> 
          <Binding Path="Start" UpdateSourceTrigger="PropertyChanged"> 
           <Binding.ValidationRules> 
            <validators:MeasurementRangeRule Min="1513" Max="1583"/> 
           </Binding.ValidationRules> 
          </Binding> 
         </TextBox.Text> 
        </TextBox> 
    
        <TextBox Name="textboxEnd" Grid.Row="1"> 
         <TextBox.Text> 
          <Binding Path="End" UpdateSourceTrigger="PropertyChanged"> 
           <Binding.ValidationRules> 
            <validators:MeasurementRangeRule Min="1513" Max="1583"/> 
           </Binding.ValidationRules> 
          </Binding> 
         </TextBox.Text> 
        </TextBox> 
    </Grid> 
    

    そしてここでは、関連するC#のコードです:

    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.Windows; 
    using System.Windows.Controls; 
    using System.Windows.Data; 
    using System.Windows.Documents; 
    using System.Windows.Input; 
    using System.Windows.Media; 
    using System.Windows.Media.Imaging; 
    using System.Windows.Navigation; 
    using System.Windows.Shapes; 
    using System.Runtime.CompilerServices; 
    using System.ComponentModel; 
    using System.Globalization; 
    
    namespace WpfApplication1 { 
        /// <summary> 
        /// Interaction logic for MainWindow.xaml 
        /// </summary> 
        public partial class MainWindow : Window { 
         public MainWindow() { 
          InitializeComponent(); 
         } 
    
         private decimal _start; 
         private decimal _end; 
         public event PropertyChangedEventHandler PropertyChanged; 
    
         public decimal Start { 
          get { return _start; } 
          set { 
           _start = value; 
           RaisePropertyChanged(); 
          } 
         } 
    
         public decimal End { 
          get { return _end; } 
          set { 
           _end = value; 
           RaisePropertyChanged(); 
          } 
         } 
    
         private void RaisePropertyChanged ([CallerMemberName] string propertyName = "") { 
          if (PropertyChanged != null) { 
           PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
          } 
         } 
        } 
    } 
    
    namespace CustomValidators { 
    
        public class MeasurementRangeRule : ValidationRule { 
         private decimal _min; 
         private decimal _max; 
    
         public decimal Min { 
          get { return _min; } 
          set { _min = value; } 
         } 
    
         public decimal Max { 
          get { return _max; } 
          set { _max = value; } 
         } 
    
         public override ValidationResult Validate (object value, CultureInfo cultureInfo) { 
          decimal measurementParameter = 0; 
    
          try { 
           if (((string) value).Length > 0) 
            measurementParameter = Decimal.Parse((String) value); 
          } catch (Exception e) { 
           return new ValidationResult(false, "Illegal characters or " + e.Message); 
          } 
    
          if ((measurementParameter < Min) || (measurementParameter > Max)) { 
           return new ValidationResult(false, 
            "Out of range. Enter a parameter in the range: " + Min + " - " + Max + "."); 
          } else { 
           return new ValidationResult(true, null); 
          } 
         } 
        } 
    } 
    

    hereリンク質問は関連があると思われるが、私は理解できません答えが提供されました。

    おかげで...この問題に直面するかもしれない人のために任意の

    +0

    あなたはIDataErrorInfoやBindingGroupで見たことがありますか? – Shoe

    +0

    @Jim IDataErrorInfoを見て、自分のクラスに関連するプロパティ(開始、終了、最小、最大)をカプセル化し、IDataErrorInfoを実装するようにコードを再構成しました。そのセットアップは魅力的なように機能し、ポインタのために非常に高く評価されました。私は明日答えを掲示するつもりです。 –

    答えて

    4

    は、一般的にエラーを検証するために、そしていくつかの論理グループ内の他のコントロールに対して、検証を達成するためにIDataErrorInfoを実装するためにはるかに簡単です。関連するプロパティ(開始、終了、最小、最大)を1つのクラスにカプセル化し、コントロールをそれらのプロパティにバインドして、IDataErrorInfoインターフェイスを検証に使用しました。関連するコードは以下の通りです...

    XAML:

    <TextBox Name="textboxStart" Grid.Row="0" Text="{Binding Path=Start, ValidatesOnDataErrors=True}" Margin="5"/> 
        <TextBox Name="textboxEnd" Grid.Row="1" Text="{Binding Path=End, ValidatesOnDataErrors=True}" Margin="5"/> 
    </Grid> 
    

    のC#:

    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.Windows; 
    using System.Windows.Controls; 
    using System.Windows.Data; 
    using System.Windows.Documents; 
    using System.Windows.Input; 
    using System.Windows.Media; 
    using System.Windows.Media.Imaging; 
    using System.Windows.Navigation; 
    using System.Windows.Shapes; 
    using System.Runtime.CompilerServices; 
    using System.ComponentModel; 
    
    namespace WpfApplication1 { 
        /// <summary> 
        /// Interaction logic for MainWindow.xaml 
        /// </summary> 
        public partial class MainWindow : Window { 
         public MainWindow() { 
          InitializeComponent(); 
    
          Parameter testParameter = new Parameter(0, 10); 
          testGrid.DataContext = testParameter; 
         } 
        } 
    
        public class Parameter: INotifyPropertyChanged, IDataErrorInfo { 
         private decimal _start, _end, _min, _max; 
         public event PropertyChangedEventHandler PropertyChanged; 
    
         public Parameter() { } 
    
         public Parameter (decimal min, decimal max) { 
          this.Min = min; 
          this.Max = max; 
         } 
    
         public decimal Start { 
          get { return _start; } 
          set { 
           _start = value; 
           //RaisePropertyChanged for both Start and End, because one may need to be marked as invalid because of the other's current setting. 
           //e.g. Start > End, in which case both properties are now invalid according to the established conditions, but only the most recently changed property will be validated 
           RaisePropertyChanged(); 
           RaisePropertyChanged("End"); 
          } 
         } 
    
         public decimal End { 
          get { return _end; } 
          set { 
           _end = value; 
           //RaisePropertyChanged for both Start and End, because one may need to be marked as invalid because of the other's current setting. 
           //e.g. Start > End, in which case both properties are now invalid according to the established conditions, but only the most recently changed property will be validated 
           RaisePropertyChanged(); 
           RaisePropertyChanged("Start"); 
          } 
         } 
    
         public decimal Min { 
          get { return _min; } 
          set { _min = value; } 
         } 
    
         public decimal Max { 
          get { return _max; } 
          set { _max = value; } 
         } 
    
         private void RaisePropertyChanged ([CallerMemberName] string propertyName = "") { 
          if (PropertyChanged != null) { 
           PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
          } 
         } 
    
         public string Error { 
          get { return string.Empty; } 
         } 
    
         public string this[string columnName] { 
          get { 
           string result = string.Empty; 
    
           switch (columnName) { 
            case "Start": 
             if (Start < Min || Start > Max || Start > End) { 
              result = "Out of range. Enter a value in the range: " + Min + " - " + End + "."; 
             } 
             break; 
            case "End": 
             if (End < Min || End > Max || End < Start) { 
              result = "Out of range. Enter a value in the range: " + Start + " - " + Max + "."; 
             } 
             break; 
           }; 
    
           return result; 
          } 
         } 
        } 
    } 
    
    関連する問題