我有一个TextBox,可以对单个属性进行验证,如下所示:
<TextBox Name="textbox_validation">
<TextBox.Text>
<Binding Path="A">
<Binding.ValidationRules>
<ExceptionValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
使用这样的属性
private double a;
public double A
{
get { return a; }
set
{
if (a != value)
{
if (value > 22 || value < 10)
{
throw new ApplicationException("Invalid value");
}
a = value;
OnPropertyChanged("A");
}
}
}
这给了我一个很好的文本框,当数字> 22且<10时,它会在它周围有一个指示(红线)。可爱。
当我将一个文本框绑定到这样的类时,我的问题出现了:
public class Derp:INotifyPropertyChanged
{
public Derp(double first, double second, bool third)
{
if (!third)
{
A = first;
C = first - (second / 2);
}
else
{
C = first;
A = first + second - first; //Please ignore all logic...
}
}
private double c
public double C
{
get { return c; }
set
{
if (c != value)
{
if (value < a)
{
throw new ApplicationException("Invalid value");
}
c = value;
OnPropertyChanged("C");
}
}
}
private double a
public double A
{
get { return a; }
set
{
if (a != value)
{
if (value > c)
{
throw new ApplicationException("Invalid value");
}
a = value;
OnPropertyChanged("A");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
我的文本框可以绑定到A或C,具体取决于checkbox_next的状态。
<TextBox Grid.Row="0" Grid.Column="1" Name="textbox_differentProperties">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Text" Value="{Binding DerpProperty.A}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=checkbox_next}" Value="True">
<Setter Property="Text" Value="{Binding DerpProperty.C}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
我想使用textbox_validation的方法实现textbox_differentProperties的验证,并按照
的方式思考<TextBox Grid.Row="0" Grid.Column="1" Name="textbox_differentProperties">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Text" Value="{Binding A}"/>
<SOME VALIDATION???/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=checkbox_next}" Value="True">
<Setter Property="Text" Value={Binding C}/>
<VALIDATIONNN???/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
如何使用datatrigger(已更改的属性)实现验证的任何线索都会非常有用。