编辑我正在添加其他代码以澄清...
我遇到的问题是IDataErrorInfo错误指示器显示在最初创建的“Collapsed”控件上,然后使用DataTrigger显示。
有一个ComboBox控件绑定到查找值,它显示三行...“Cash”,“Credit Card”和“Check”。 我有两个TextBox控件分别绑定到“CreditCardCode”和“CheckNumber”。
TextBox控件设置了一个DataTrigger,只有在选择了相关的ComboBox值时才能使它们可见。如果相关的TextBox没有值,则数据错误逻辑将显示错误。
这是组合框和文本框的XAML:
<ComboBox IsEditable="False"
IsSynchronizedWithCurrentItem="False"
SelectedItem="{Binding Path=PaymentType}"
ItemsSource="{Binding Source={StaticResource paymentTypeLookup}}"
DisplayMemberPath="{Binding PaymentTypeCode}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=PaymentTypeCode,
ValidatesOnDataErrors=True}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ComboBox>
<TextBox Name="CCCodeText"
Text="{Binding CreditCardCode, ValidatesOnDataErrors=True,
ValidatesOnExceptions=True, NotifyOnValidationError=True}" >
<TextBox.Style>
<Style TargetType="TextBox">
<Setter Property="TextBox.Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=PaymentComboBox,
Path=SelectedItem.PaymentTypeCode}"
Value="CC_SWIPE">
<Setter Property="TextBox.Visibility"
Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
以下是PaymentType对象的代码
public virtual PaymentType PaymentType
{
get { return _paymentType; }
set
{
if (!ReferenceEquals(_paymentType, value))
{
var previousValue = _paymentType;
_paymentType = value;
FixupPaymentType(previousValue);
NotifyPropertyChanged("PaymentType");
OnPaymentTypeChanged(value);
}
}
}
private PaymentType _paymentType;
这是OnPaymentTypeChanged()函数:
void OnPaymentTypeChanged(PaymentType value)
{
errors.Remove("CheckNumber"); //Avoid Dupes
errors.Remove("CreditCardCode"); //Avoid Dupes
if (this.PaymentType != null)
{
if (value.PaymentTypeCode == "CHECK" &&
(this.CheckNumber == String.Empty || this.CheckNumber == null))
errors.Add("CheckNumber", "Check Number required");
else if (value.PaymentTypeCode == "CC_SWIPE"
&& (this.CreditCardCode == String.Empty ||
this.CreditCardCode == null))
errors.Add("CreditCardCode", "Credit Card Code required");
}
}
如果在首次显示窗口时将ComboBox设置为“信用卡”(并且CCCodeText控件可见),则默认的红色边框显示正常。但是,如果我将ComboBox的值更改为“Check”,并且出现CheckTextBox控件,则它将缺少红色边框。
当我将ComboBox更改回“信用卡”时,红色边框仍然存在。
我在哪里错过了刷新属性???