2013-09-01 7 views
8

मैं ऐसा कुछ करने की कोशिश कर रहा हूं जिसे मैंने पहले माना था कि यह काफी आसान होगा: दूसरे के सत्यापन नियम में एक नियंत्रण से मूल्य का उपयोग करें। मेरे एप्लिकेशन में विभिन्न प्रकार के पैरामीटर हैं जो उपयोगकर्ता दर्ज कर सकते हैं, प्रश्न में विशिष्ट पैरामीटर एक श्रेणी के प्रारंभ और अंत बिंदु को परिभाषित करते हैं, और उपयोगकर्ता टेक्स्टबॉक्स के माध्यम से मान सेट करता है। सवाल मेंकिसी अन्य नियंत्रण से मूल्य का उपयोग करके सत्यापन नियम

दो नियंत्रण आरंभ और अंत बक्सें हैं, और निम्न स्थितियों के सत्यापन में जाँच की जानी चाहिए:

  1. प्रारंभ मूल्य कुछ मनमाना मूल्य
  2. समाप्ति मूल्य चाहिए से अधिक या बराबर होना चाहिए हो से या कुछ मनमाना मूल्य
  3. प्रारंभ मूल्य मूल्य

पहले दो की स्थिति मैं पहले से ही accompl है समाप्त करने के लिए बराबर या उससे कम होना चाहिए के बराबर कम दंडित। तीसरा कार्यान्वित करना कहीं अधिक कठिन है, क्योंकि मैं वैधकर्ता से अंत टेक्स्टबॉक्स के मूल्य तक नहीं पहुंच सकता। यहां तक ​​कि अगर मैं कर सकता हूं, तो पांच अलग-अलग श्रेणियां हैं (प्रत्येक अपनी शुरुआत और अंत टेक्स्टबॉक्स के साथ) मैं मान्य करने की कोशिश कर रहा हूं, और प्रत्येक के लिए सत्यापन नियम बनाने से कुछ समाधान अधिक सुरुचिपूर्ण होना चाहिए। यहाँ

<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> 

और प्रासंगिक सी # कोड है::

यहाँ प्रासंगिक XAML कोड है

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 लागू करने के लिए सामान्य रूप में त्रुटियों को मान्य करने, और कुछ तर्कसंगत समूहन में अन्य नियंत्रण से सत्यापन पूरा करने के लिए आसान है। मैंने एक ही वर्ग में प्रासंगिक गुणों (प्रारंभ, अंत, न्यूनतम और अधिकतम) को समाहित किया, उन गुणों के नियंत्रण को बाध्य किया, और फिर सत्यापन के लिए 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> 

सी #:

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; 
      } 
     } 
    } 
} 
संबंधित मुद्दे