在自己的控制中传递验证错误
我有自己的用户控件:
[TemplateVisualState(Name = StateValid, GroupName = GroupValidation)]
[TemplateVisualState(Name = StateInvalidFocused, GroupName = GroupValidation)]
[TemplateVisualState(Name = StateInvalidUnfocused, GroupName = GroupValidation)]
public class SearchTextBoxControl : TextBox
{
// properties removed for brevity
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.BindingValidationError += (s, e) => UpdateValidationState();
this.UpdateValidationState();
}
public const string GroupValidation = "ValidationStates";
public const string StateValid = "Valid";
public const string StateInvalidFocused = "InvalidFocused";
public const string StateInvalidUnfocused = "InvalidUnfocused";
private void UpdateValidationState()
{
var textBox = this.GetTemplateChild("ContentTextBox");
if (textBox != null)
{
VisualStateManager
.GoToState(textBox as Control,
Validation.GetErrors(this).Any() ?
StateInvalidUnfocused :
StateValid,
true);
}
}
}
和 XAML:
<Style TargetType="local:SearchTextBoxControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:SearchTextBoxControl">
<Grid Grid.Column="1"
Grid.ColumnSpan="3"
Grid.Row="1"
Margin="{TemplateBinding Margin}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="32" />
</Grid.ColumnDefinitions>
<TextBox x:Name="ContentTextBox"
Grid.ColumnSpan="2"
IsReadOnly="{TemplateBinding IsReadOnly}"
Text="{TemplateBinding Text}">
</TextBox>
<Button Grid.Column="1"
Style="{StaticResource BrowseButton}"
Command="{TemplateBinding Command}">
<ToolTipService.ToolTip>
<ToolTip Content="{TemplateBinding ToolTip}" />
</ToolTipService.ToolTip>
<Image Source="../../Resources/Images/magnifier.png"
Style="{StaticResource BrowseButtonImage}" />
</Button>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我应该如何将验证错误传递到 TextBox x:Name="ContentTextBox" 验证服务(我希望控件文本框中有相同的验证错误工具提示)? 亲切的问候!
I've got my own user control:
[TemplateVisualState(Name = StateValid, GroupName = GroupValidation)]
[TemplateVisualState(Name = StateInvalidFocused, GroupName = GroupValidation)]
[TemplateVisualState(Name = StateInvalidUnfocused, GroupName = GroupValidation)]
public class SearchTextBoxControl : TextBox
{
// properties removed for brevity
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.BindingValidationError += (s, e) => UpdateValidationState();
this.UpdateValidationState();
}
public const string GroupValidation = "ValidationStates";
public const string StateValid = "Valid";
public const string StateInvalidFocused = "InvalidFocused";
public const string StateInvalidUnfocused = "InvalidUnfocused";
private void UpdateValidationState()
{
var textBox = this.GetTemplateChild("ContentTextBox");
if (textBox != null)
{
VisualStateManager
.GoToState(textBox as Control,
Validation.GetErrors(this).Any() ?
StateInvalidUnfocused :
StateValid,
true);
}
}
}
and XAML:
<Style TargetType="local:SearchTextBoxControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:SearchTextBoxControl">
<Grid Grid.Column="1"
Grid.ColumnSpan="3"
Grid.Row="1"
Margin="{TemplateBinding Margin}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="32" />
</Grid.ColumnDefinitions>
<TextBox x:Name="ContentTextBox"
Grid.ColumnSpan="2"
IsReadOnly="{TemplateBinding IsReadOnly}"
Text="{TemplateBinding Text}">
</TextBox>
<Button Grid.Column="1"
Style="{StaticResource BrowseButton}"
Command="{TemplateBinding Command}">
<ToolTipService.ToolTip>
<ToolTip Content="{TemplateBinding ToolTip}" />
</ToolTipService.ToolTip>
<Image Source="../../Resources/Images/magnifier.png"
Style="{StaticResource BrowseButtonImage}" />
</Button>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
What should I do to pass validation errors to the TextBox x:Name="ContentTextBox" validation service (I want the same validation error tooltip on my control text box)?
Kind regards!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以实现 IDataErrorInfo 接口。它可以在 ComponentModel Lib 中找到。
使用 System.ComponentModel;
公共类 SearchTextBoxControl : TextBox , IDataErrorInfo
{
}
You can implement IDataErrorInfo Interface. It is available in ComponentModel Lib.
using System.ComponentModel;
public class SearchTextBoxControl : TextBox , IDataErrorInfo
{
}