大家好, 我具有以下验证错误样式,该样式可与文本框完美配合使用,但无法与密码框一起使用.请让我知道此XAML的问题是什么,或者您是否具有用于设置密码验证样式的有用链接. 这里是XAML:
<
样式 TargetType =&; { x : 类型 PasswordBox }">
< Setter 属性 =" Validation.ErrorTemplate>
< Setter.Value >
< ControlTemplate >
< DockPanel LastChildFill =" True>
< TextBlock DockPanel.Dock ="Right" 前景 ="#FF9F513E" FontSize =" 20" 边距 =" 3,0,0,0" FontWeight =粗体" FontFamily ="webdings">< 运行 文本 =&q uot; i"/></ TextBlock >
< 边框 BorderBrush ="#FF9F513E" BorderThickness ="0" 背景 ="#19FFC1C1" IsHitTestVisible =" False">
</ 边框 >
</ DockPanel >
</ ControlTemplate >
</ Setter.Value >
</ Setter >
< Style.Triggers >
< 触发 属性 =" Validation.HasError" 值 =" True>
< Setter 属性 =" ToolTip" 值 =&"; { 绑定 路径 =(验证错误)[ 0 ].ErrorContent, RelativeSource = { RelativeSource 自我 }}" />
</ 触发 >
</ Style.Triggers >
</ 样式 > 提前感谢...
解决方案首先,由于Validation.Errors并不总是如此,因此该技术会给您带来绑定错误.可用(请仔细查看您的输出窗口).要解决此问题,您可以使用自定义转换器: ///< summary> ///转换一个包含< see cref ="ValidationError"/>对象返回第一个错误 ///或一个空字符串,以防出现错误. ///</summary> [ValueConversion(typeof(ICollection< ValidationError>),typeof(string))] 公共类GetFirstValidationErrorConverter:IValueConverter { ///< summary> ///转换一个包含< see cref ="ValidationError"/>对象返回第一个错误 ///或一个空字符串,以防出现错误. ///</summary> ///< param name =值">要检查的值. </param>同时支持集合和数组. ///< param name ="targetType">未使用. ///< param name =参数">未使用. ///< param name =文化">未使用.</param> ///< returns>包含第一个错误的字符串,或者<参见cref ="string.Empty"//在没有错误的情况下. 公共对象Convert(对象值,类型targetType,对象参数,System.Globalization.CultureInfo文化) { //检查我们是否有一个有效值 if(value == null)返回string.Empty; //转换值 ICollection< ValidationError> errorCollection =值为ICollection< ValidationError> ;; 如果(errorCollection == null)返回string.Empty; //返回错误 返回(errorCollection.Count> 0)吗? errorCollection.First().ErrorContent.ToString():string.Empty; } ///< summary> ///不支持此方法. ///</summary> ///< param name =值">未使用.</param> ///< param name ="targetType">未使用. ///< param name =参数">未使用. ///< param name =文化">未使用.</param> ///< returns>始终为null.</returns> 公共对象ConvertBack(对象值,类型targetType,对象参数,System.Globalization.CultureInfo文化) { 返回null; //无法进行转换 } }
< Setter属性="ToolTip"值="{Binding RelativeSource = {RelativeSource Self},Path =(Validation.Errors),Converter = {StaticResource GetFirstValidationErrorConverter}}"" />
Hi all,I have the following style for validation error that works perfect with text box , but cann't get it working with the passwordbox. Please let me know what is the problem with this XAML , or if you have useful links for styling password validation.Here is the XAML:
<
Style TargetType="{x:Type PasswordBox}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Right" Foreground="#FF9F513E" FontSize="20" Margin="3,0,0,0" FontWeight="Bold" FontFamily="Webdings"><Run Text="i"/></TextBlock>
<Border BorderBrush="#FF9F513E" BorderThickness="0" Background="#19FFC1C1" IsHitTestVisible="False">
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="ToolTip" Value="{Binding Path=(Validation.Errors)[0].ErrorContent, RelativeSource={RelativeSource Self}}" />
</Trigger>
</Style.Triggers>
</Style>Thanks in advance...
解决方案 First of all, this technique will give you binding errors since Validation.Errors is not always available (take a good look at your output window). To fix this, you can use a custom converter: /// <summary> /// Converts a collection containing <see cref="ValidationError"/> objects to return the first error /// or an empty string in case there are no errors. /// </summary> [ValueConversion(typeof(ICollection<ValidationError>), typeof(string))] public class GetFirstValidationErrorConverter : IValueConverter { /// <summary> /// Converts a collection containing <see cref="ValidationError"/> objects to return the first error /// or an empty string in case there are no errors. /// </summary> /// <param name="value">Values to check for. Both collections and arrays are supported.</param> /// <param name="targetType">Not used.</param> /// <param name="parameter">Not used.</param> /// <param name="culture">Not used.</param> /// <returns>String containing the first error or <see cref="string.Empty"/> in case there are not errors.</returns> public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { // Check if we have a valid value if (value == null) return string.Empty; // Convert value ICollection<ValidationError> errorCollection = value as ICollection<ValidationError>; if (errorCollection == null) return string.Empty; // Return error return (errorCollection.Count > 0) ? errorCollection.First().ErrorContent.ToString() : string.Empty; } /// <summary> /// This method isn't supported. /// </summary> /// <param name="value">Not used.</param> /// <param name="targetType">Not used.</param> /// <param name="parameter">Not used.</param> /// <param name="culture">Not used.</param> /// <returns>Always null.</returns> public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return null; // no conversion possible } }<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors), Converter={StaticResource GetFirstValidationErrorConverter}}" />
更多推荐
密码验证错误样式
发布评论