C# 绑定到(Validation.Errors)。CurrentItem不适用于TextBox(适用于DataGrid)

C# 绑定到(Validation.Errors)。CurrentItem不适用于TextBox(适用于DataGrid),c#,wpf,validation,binding,C#,Wpf,Validation,Binding,我正在尝试将工具提示属性绑定到代码中的(Validation.Errors).CurrentItem。我用DataGrid做到了这一点,比如: var grid = new FrameworkElementFactory(typeof(Grid)); grid.SetValue(Grid.ToolTipProperty, new Binding() { RelativeSource = new RelativeSource(RelativeSourceMode.FindAnces

我正在尝试将工具提示属性绑定到代码中的(Validation.Errors).CurrentItem。我用DataGrid做到了这一点,比如:

var grid = new FrameworkElementFactory(typeof(Grid));
grid.SetValue(Grid.ToolTipProperty, new Binding() {
        RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(DataGridRow), 1),
        Path = new PropertyPath("(Validation.Errors).CurrentItem.ErrorContent")
});
这样,错误符号就会出现在带有工具提示的行标题中(一些错误文本)

当我尝试对文本框执行相同操作时,不会显示工具提示:

grid.SetValue(Grid.ToolTipProperty, new Binding() {
     ElementName = textBox1.Name, // tried with relative source also...
     Path = new PropertyPath("(Validation.Errors).CurrentItem.ErrorContent")
});
问候,


Vale

看起来您可以使用该属性。像这样:

grid.SetValue(Grid.ToolTipProperty, new Binding() {
     Source = textBox1,
     Path = new PropertyPath("(Validation.Errors).CurrentItem.ErrorContent")
});

我不确定您是否需要其中的当前项

我以前从未在代码隐藏中进行过验证绑定,但我的用于文本框验证的XAML如下所示:

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


文本框是在网格内(我是说,在单元格内)还是在网格外?网格只是错误符号的基础。所以工具提示在一个网格上…是的,它绑定到一个文本框本身,但我想绑定到错误符号(里面有椭圆和文本块的网格,看起来像(!)放在附近的TextBox@Vale在触发器中设置模板而不是工具提示,并覆盖它以显示网格和Ellipse@Rachel:CurrentItem允许您在没有验证错误时在控制台中绑定而不获取错误。看见