4

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

ILMTitan
  • 10,751
  • 3
  • 30
  • 46
Vale
  • 3,258
  • 2
  • 27
  • 43

2 Answers2

3

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>
Rachel
  • 130,264
  • 66
  • 304
  • 490
  • Yes, that is binding to a TextBox itself, but I want to bind to error sign (grid with Ellipse and TextBlock inside, looks like (!) ) that is placed near TextBox – Vale Mar 30 '11 at 16:14
  • @Vale Set the Template in the Trigger instead of the ToolTip and overwrite it to show your Grid and Ellipse – Rachel Mar 30 '11 at 17:09
  • 2
    @Rachel: CurrentItem lets you bind and not get errors in the console when there are no validation errors. See http://stackoverflow.com/questions/2260616/why-does-wpf-style-to-show-validation-errors-in-tooltip-work-for-a-textbox-but-f – yagni Oct 09 '12 at 17:51
2

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")
});
CodeNaked
  • 40,753
  • 6
  • 122
  • 148