Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remaining validation tooltip on TabControl

I have a problem with a TabControl, a TextBox and a validation ToolTip.

Imagine having a TabControl with two TabItems. On the first item there is a simple TextBox. This TextBox Text property is bound to a string property of the UserControl itself with Mode=TwoWay and ValidatesOnExceptions=True. The setter of that Text property throws an exception whenever something is set.

The Resources section of the UserControl contains a new default style for the TextBox and the validation ToolTip (those styles and templates however are taken from the MSDN).

Now enter something into the TextBox and let the validation ToolTip appear:

enter image description here

Then change to the second tab. The validation ToolTip remains:

enter image description here

I have produced a VS solution containing a Silverlight application that demonstrates the issue. The VS solution zip archive is available here.

Has anyone had similar problems or even a solution for that issue?

Disclaimer: There is similar question here on StackOverflow with regard to Silverlight 4 which has been unanswered since about one and a half years. I already posted that question on silverlight.net but got no replies for several days.

like image 407
Spontifixus Avatar asked Jul 12 '12 15:07

Spontifixus


1 Answers

I think this is a bug of the TabControl-implementation. I've implemented the this behavior to fix this in our application:

public class TabControlFixBehavior: Behavior<TabControl>
{
    protected override void OnAttached()
    {
        AssociatedObject.SelectionChanged += AssociatedObjectOnSelectionChanged;
        base.OnAttached();
    }

    protected override void OnDetaching()
    {
        AssociatedObject.SelectionChanged -= AssociatedObjectOnSelectionChanged;

        base.OnDetaching();
    }

    private void AssociatedObjectOnSelectionChanged(object sender, SelectionChangedEventArgs args)
    {
        if (args.RemovedItems.Count > 0)
        {
            var oldTabItem = args.RemovedItems[0] as TabItem;
            if (oldTabItem != null)
            {
                var popups = VisualTreeHelper.GetOpenPopups();
                foreach (var popup in popups)
                {
                    var toolTip = popup.Child as ToolTip;
                    if (toolTip != null)
                    {
                        if (VisualTreeHelper.GetRoot(toolTip.PlacementTarget) == oldTabItem.Content)
                        {
                            popup.IsOpen = false;
                        }
                    }
                }
            }
        }
    }
}
like image 89
Andir Avatar answered Sep 28 '22 00:09

Andir