简体   繁体   中英

Binding to (Validation.Errors).CurrentItem does not work with TextBox (works with DataGrid)

I am trying to bind ToolTip property to a (Validation.Errors).CurrentItem in code. I allready did that with DataGrid like:

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")
});

That works, error sign appears in row header with tool tip (some error text).

When I try to do the same with text box tool tip does not appear:

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

Regards,

Vale

Looks like you may be able to use the Binding.Source property. Like so:

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

I'm not sure that you need the CurrentItem in there

I've never done validation binding in code behind before, but my XAML for TextBox validation looks like this:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM