C# 错误消息仅显示错误消息中的第一个字符。INotifyDataErrorInfo WPF

C# 错误消息仅显示错误消息中的第一个字符。INotifyDataErrorInfo WPF,c#,wpf,wpf-style,inotifydataerrorinfo,C#,Wpf,Wpf Style,Inotifydataerrorinfo,当文本框为空时,我有一个简单的验证来显示错误消息。 问题在于消息仅显示消息的第一个字母 在文本框样式中: <Trigger Property="Validation.HasError" Value="True"> <Setter Property="ToolTip" Value="{Binding Path=(Validation.Errors)[0].ErrorContent, RelativeSource={x:Static RelativeSource.Self

当文本框为空时,我有一个简单的验证来显示错误消息。 问题在于消息仅显示消息的第一个字母

在文本框样式中:

<Trigger Property="Validation.HasError" Value="True">
    <Setter Property="ToolTip" Value="{Binding Path=(Validation.Errors)[0].ErrorContent, RelativeSource={x:Static RelativeSource.Self}}" />
</Trigger>

如果我将错误消息直接设置为Setter值,那么它将毫无问题地显示所有错误消息

<Trigger Property="Validation.HasError" Value="True">
    <Setter Property="ToolTip" Value="This field is required!" />
</Trigger>

XAML代码:

<TextBox Text="{Binding Name, Mode=TwoWay,
                UpdateSourceTrigger=PropertyChanged, 
                ValidatesOnNotifyDataErrors=True,
                NotifyOnValidationError=True}" />

C#代码

private readonly Dictionary\u errors=new Dictionary();
私有只读对象_lock=新对象();
公共事件事件处理程序错误更改;
公共IEnumerable GetErrors(字符串propertyName)
{
字符串错误名称;
锁
{
errorsForName=\u errors.FirstOrDefault(e=>e.Key==propertyName).Value;/.TryGetValue(propertyName,OutErrorsForName);
}
返回错误名称;
}
公共布尔错误
{
获取{return\u errors.Values.FirstOrDefault(l=>!String.IsNullOrEmpty(l))!=null;}
}
私有void raiserRorschanged(字符串属性名称)
{
EventHandler=ErrorsChanged;
if(handler==null)返回;
var arg=新数据错误SchangedEventArgs(propertyName);
调用(this,arg);
}
私有void RequiredValidation(字符串属性名称、字符串值)
{
锁
{
if(String.IsNullOrWhiteSpace(value))
{
_错误[propertyName]=“名称不能为null或空。”;
}
其他的
{
如果(_errors.ContainsKey(propertyName)){u errors.Remove(propertyName);}
}
RaiserRorschanged(propertyName);
SaveCommand.RaiseCanExecuteChanged();
}
}

我在我的文本框样式上尝试了你的触发器,没有任何问题,工具提示正确呈现

请确认您正在实现IDataErrorInfo并在IDataErrorInfo属性中返回有效字符串:

        public string this[string columnName]
        {
            get { return ValidateInput(columnName); }
        }
我的示例项目中的触发器结果:


您的
GetErrors
方法应该返回
IEnumerable
而不是
IEnumerable

公共IEnumerable GetErrors(字符串propertyName) { 字符串错误名称; 锁 { errorsForName=\u errors.FirstOrDefault(e=>e.Key==propertyName).Value;/.TryGetValue(propertyName,OutErrorsForName); } 返回新列表{errorsForName}; }
如果表单中有其他不需要验证的控件,请确保返回空值。事实上,实例化列表不是空的,如果您在_errorsdictionary中有某些内容时单击它,则会导致在未验证的控件上显示红色边框

public IEnumerable GetErrors(string propertyName)
{
    string errorsForName;
    lock (_lock)
    {
        errorsForName = _errors.FirstOrDefault(e => e.Key == propertyName).Value;//.TryGetValue(propertyName, out errorsForName);
    }
    if (String.IsNullOrEmpty(errorsForName)) return null;
    return new List<string> { errorsForName };
}
公共IEnumerable GetErrors(字符串propertyName) { 字符串错误名称; 锁 { errorsForName=\u errors.FirstOrDefault(e=>e.Key==propertyName).Value;/.TryGetValue(propertyName,OutErrorsForName); } if(String.IsNullOrEmpty(errorsForName))返回null; 返回新列表{errorsForName}; }
我使用的是INotifyDataErrorInfo,字符串返回正确的值,我更改了消息的第一个字母,它在工具提示中也相应更改。我已经更新了问题,加入了c代码
public IEnumerable GetErrors(string propertyName)
{
    string errorsForName;
    lock (_lock)
    {
        errorsForName = _errors.FirstOrDefault(e => e.Key == propertyName).Value;//.TryGetValue(propertyName, out errorsForName);
    }
    return new List<string> { errorsForName };
}
public IEnumerable GetErrors(string propertyName)
{
    string errorsForName;
    lock (_lock)
    {
        errorsForName = _errors.FirstOrDefault(e => e.Key == propertyName).Value;//.TryGetValue(propertyName, out errorsForName);
    }
    if (String.IsNullOrEmpty(errorsForName)) return null;
    return new List<string> { errorsForName };
}