我TextBox
绑定了一个对象的属性,该对象实现了IDataErrorInfo
. 我设置了Validation.ErrorTemplate
,TextBox
它工作正常。问题是我在 a 上有这些TabControl
,如果我将选项卡更改为另一个选项卡然后返回到初始选项卡(在哪里),验证模板将不再显示TextBox
。看起来它已经过验证(就像值是正确的),但实际上不是。
这是IDataErrorInfo
对象 - 请注意,“正确”值是长度为 2 的字符串:
public class Presenter : IDataErrorInfo
{
public Presenter()
{
this.Property = String.Empty;
}
public string Property { get; set; }
public string Error { get { return null; } }
public string this[string columnName]
{
get
{
if (columnName == "Property")
{
if (this.Property.Length == 2)
return null;
else
return "Invalid property length!";
}
else return null;
}
}
}
这是 XAML:
<TabControl >
<TabItem Header="tabItem1" Name="tabItem1" GotFocus="tabItem1_GotFocus">
<Grid>
<TextBox Width="100" Height="20" x:Name="txtField">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="16"/>
</Grid.ColumnDefinitions>
<AdornedElementPlaceholder Grid.Column="0"/>
<Image Source="bullets16.png" Grid.Column="1" ToolTip="{Binding CurrentItem.ErrorContent, Mode=OneWay}">
</Image>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TextBox.Style>
<TextBox.Text>
<Binding Path="Property" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True">
</Binding>
</TextBox.Text>
</TextBox>
</Grid>
</TabItem>
<TabItem Header="tabItem2" Name="tabItem2" >
<Grid />
</TabItem>
</TabControl>
关于我做错了什么的任何想法?