【发布时间】:2013-01-21 14:16:34
【问题描述】:
我想验证 WPF 上的 TextBox。我正在使用Binding 来验证它:
<TextBox Text="{Binding Path=Name, UpdateSourceTrigger=Explicit}" TabIndex="0" LostFocus="TextBox_OnLostFocus">
</TextBox>
LostFocus 事件:
private void TextBox_OnLostFocus(object sender, RoutedEventArgs e)
{
((Control) sender).GetBindingExpression(TextBox.TextProperty);
}
验证背后的代码:
public string Name
{
get { return _name; }
set
{
_name = value;
this.OnPropertyChanged(new PropertyChangedEventArgs("Name"));
}
}
public string Error { get { return this[null]; } }
public string this[string columnName]
{
get
{
string result = string.Empty;
columnName = columnName ?? string.Empty;
if (columnName == string.Empty || columnName == "Name")
{
if (string.IsNullOrEmpty(this.Name))
{
result += Properties.Resources.ValidationName + Environment.NewLine;
}
}
return result.TrimEnd();
}
}
我有一些问题:
1.当我第一次加载我的窗口时,我的控件被一个红色方块(验证方块)包围,但我希望它仅在我触发它时出现(在Explicit边)。
2. 我如何知道我的所有字段是否都已通过验证?我的意思是,当我按下按钮时,我只需要知道如何知道所有控件是否都已被验证。
注意:我在构造函数上有这个上下文:
User u = new User();
DataContext = u;
【问题讨论】:
标签: c# wpf validation textbox wpf-controls