如何使顶级用户控件Validation.HasErrors与子TextBox.Validation.HasErrors相匹配? [英] How to make Top Level user control Validation.HasErrors match the child TextBox.Validation.HasErrors?

查看:157
本文介绍了如何使顶级用户控件Validation.HasErrors与子TextBox.Validation.HasErrors相匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UserControl ,其中带有一个 TextBox .我想在 UserControl级别(如果在文本框级别出现错误).并在TexBox级别显示错误消息.

I have a UserControl with a TextBox inside. I want to show a message at the UserControl level when there is an error at the TextBox Level. And show too an error message at TexBox level.

我们没有在控件内部使用MVVM,它的所有属性都是DependencyProperties.

We are not using MVVM inside the control, all its properties are DependencyProperties.

但是,这是一个可重用的控件,使用它的人很可能会自己使用MVVM.

However, this is a reusable control, and the people using it, will be likely using MVVM themselves.

他们将像这样将其添加到其视图中:

They would add it to their View like this:

<myUserControlNamespace:MyUserControl x:Name="control1" Value="{Binding Value}" OtherProperty="{Binding OtherValue}" />

我的控件内部有一个TexBox,绑定到"Value".值是Double,因此,当用户在TexBox上输入字母时,将引发错误.我的文本框的绑定类似于以下内容:

My Control inside has a TexBox, which is binded to "Value". Value it is a Double, so when the user inputs a letter on the TexBox an error is thrown. My TextBox is binded similar to this:

<TextBox Grid.Column="1" x:Name="valueTextBox" Style="{DynamicResource ValidatingTextBox}"                 
                Validation.ValidationAdornerSite="{Binding ElementName=valueTextBox}" 
                Validation.ValidationAdornerSiteFor="{Binding}" >
	<TextBox.Text>
		<MultiBinding Converter="{StaticResource MyConverter}"Mode="TwoWay" >
			<Binding RelativeSource="{RelativeSource AncestorType={x:Type unitConversion:UnitConversionControl}}" UpdateSourceTrigger="PropertyChanged" Path="Value" Mode="TwoWay" />
			<Binding RelativeSource="{RelativeSource AncestorType={x:Type unitConversion:UnitConversionControl}}" Path="OtherProperty"/>                    
		</MultiBinding>
	</TextBox.Text>
</TextBox>

样式验证TexBox具有以下内容:

The Style Validating TexBox has something like this:

<Style x:Key="ValidatingTextBox" TargetType="{x:Type TextBox}">
	<Setter Property="Validation.ErrorTemplate">
		<Setter.Value>
			<ControlTemplate>
				<StackPanel Orientation="Vertical">
					<AdornedElementPlaceholder />
					<TextBlock Foreground="Red" Text="{Binding ErrorContent}"  Height="16" Margin="2"/>
				</StackPanel>
			</ControlTemplate>
		</Setter.Value>
	</Setter>
	<Style.Triggers>
		<Trigger Property="Validation.HasError" Value="True">
			<Setter Property="Background"
				Value="LightPink" />
			<Setter Property="Foreground"
				Value="Black" />
			<Setter Property="Margin" Value="5,5,5,28"/>
			<Setter Property="ToolTip"
				Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
		</Trigger>
	</Style.Triggers>
</Style>

我希望客户端的用户以与使用TextBox相似的方式来使用控件.设置MyUserControl样式,并使用Validation.ErrorTemplate显示错误.

I would like the users of my client to use my control in a similar way that I use the TextBox. Setting MyUserControl style, and using the Validation.ErrorTemplate to show the errors.

当TexBox中出现错误时,我希望我的控件也出现错误,因此使用我的控件的人可以处理它,例如通过使用Validation.ErrorTemplate.

When there is an error in the TexBox I would like my Control to have an error too, so the people using my control could handle it, for example by using an Validation.ErrorTemplate.

有没有办法"层叠"?错误?

Is there a way to "cascade up" the errors?

我阅读了此 问题,他使用的是 INotifyDataErrorInfo ,我不能,因为我使用的是.Net 4.0,但是我读到我可以改用IDataErrorInfo.我将不得不使我的控件实现IDataErrorInfo.这是正确的吗?

I read this SO question, he is using INotifyDataErrorInfo, I can't because I am using .Net 4.0, but I read that I could instead use IDataErrorInfo. I would have to make my control implement IDataErrorInfo. Is this correct?

谢谢!

Dzyann .-

Dzyann.-

推荐答案

Hi

了解有关msdn上的Validation.Error事件的信息.它是一个路由事件,当内部发生错误时,需要在UserControl级别捕获该事件,以便得到通知.

Read about Validation.Error event on msdn. Its a routed event that will need to catch at your UserControl level to get notified when errors happened inside.


这篇关于如何使顶级用户控件Validation.HasErrors与子TextBox.Validation.HasErrors相匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆