【发布时间】:2015-10-12 14:02:36
【问题描述】:
我有一个只包含一个标签和一个文本框的用户控件。我想使用 IDataErrorInfo 接口验证我的视图模型中的文本框条目。验证开始,但错误未显示在我的用户控件内的 TextBox 上,而是显示在用户控件本身上。
这是用户控件(其他依赖属性在基类中):
<cc:LabelableInputControl>
<Grid x:Name="LayoutRoot" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Content="{Binding Path=LabelText, FallbackValue=Empty Label}"
Grid.Column="0"
HorizontalAlignment="Left"
Name="BaseLabel"
VerticalAlignment="Center"
Width="{Binding Path=LabelWidth}"/>
<TextBox Grid.Column="1"
x:Name="BaseTextBox"
Text="{Binding Text" />
</Grid>
</cc:LabelableInputControl>
这是后面的代码:
public partial class LabelableTextBox : LabelableInputControl
{
[Bindable(true)]
public string Text
{
get { return (string)GetValue(TextProperty); }
set
{
SetValue(TextProperty, value);
}
}
public static readonly DependencyProperty TextProperty = TextBox.TextProperty.AddOwner(typeof(LabelableTextBox));
public LabelableTextBox()
{
InitializeComponent();
LayoutRoot.DataContext = this;
}
}
这就是它的(假定)使用方式:
<cc:LabelableTextBox LabelText="{x:Static text:Resources.Label_Username}"
Text="{Binding Username, Mode=TwoWay,
ValidatesOnDataErrors=True, NotifyOnValidationError=True}" />
我的问题是:如何将验证错误“转发”到 TextBox?目前,验证错误是这样显示的,但我不希望整个用户控件都被框起来。
谢谢!
【问题讨论】:
标签: .net wpf wpf-controls