私はこれまで簡単に想定していたことをしようとしています。別のコントロールの検証ルールで1つのコントロールの値を使用します。私のアプリケーションには、ユーザーが入力できるさまざまなパラメーターがあります。ここでの特定のパラメーターは、範囲の開始点と終了点を定義し、ユーザーはテキストボックスを使用して値を設定します。問題の他のコントロールの値を使用する検証ルール
2つのコントロールは、開始と終了のテキストボックスで、次の条件が検証で確認する必要があります:
- 開始値が
- 終了値がなければならないいくつかの任意の値以上でなければなりません私はすでにaccompl有する最初の2つの条件
未満またはいくつかの任意の値に等しい
は、関連する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リンク質問は関連があると思われるが、私は理解できません答えが提供されました。
おかげで...この問題に直面するかもしれない人のために任意の
あなたはIDataErrorInfoやBindingGroupで見たことがありますか? – Shoe
@Jim IDataErrorInfoを見て、自分のクラスに関連するプロパティ(開始、終了、最小、最大)をカプセル化し、IDataErrorInfoを実装するようにコードを再構成しました。そのセットアップは魅力的なように機能し、ポインタのために非常に高く評価されました。私は明日答えを掲示するつもりです。 –