我有一个绑定到实现了
这是
IDataErrorInfo
接口的对象属性的 TextBox
。我设置了 Validation.ErrorTemplate
,它可以正常工作。问题是,我把它们放在了一个 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>
有什么想法,我做错了什么吗?
Expander
内容的一个问题,同样的解决方案适用。 - Robert RossneyINotifyDataErrorInfo
而不是IDataErrorInfo
时遇到了这个问题,这个解决方案非常适合我的用例。 - blins