views:

68

answers:

1

In a Silverlight application I have a custom control with a number of custom properties. In the declaration class of the custom control additionally to defining its properties as dependency properties, I define showing a ToolTip:

public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        Border bordercntr = base.GetTemplateChild("PART_SBorder") as Border;
        bordercntr.MouseEnter += new MouseEventHandler(bordercntr_MouseEnter);
        bordercntr.MouseLeave += new MouseEventHandler(bordercntr_MouseLeave);

    }

    private void bordercntr_MouseEnter(object sender, MouseEventArgs e)
    {
       string _sno = this.SomeProperty.ToString();

       ToolTipService.SetToolTip(this, "Some text " + _sno);
       VisualStateManager.GoToState(this, "Hovered",false);                            
    }

The problem is, that a ToolTip pops up not the first time mouse points to a custom control, but only after the second time. After I reload page this happens again: the first time I hover over a control nothing is shown up and then from the second time and further on the ToolTip pops up again. (not always in a stable way, I mean not 100% each time mouse hovers).

What could prevent the ToolTip from showing up in a stable manner each time a mouse hovers over control and starting showing up from the very first time of hovering it after reloading the page?

+1  A: 

Set the ToolTip in the setter for SomeProperty The ToolTip you define in the ToolTipService will behave like a normal ToolTip and only appear when the mouse is over the control. You shouldn't need to handle the MouseEnter and MouseLeave events at all.

Stephan
Thanks, but what if I need to combine the text for the ToolTip out of several properties of the control? Where in this case should I put the ToolTip definition?
rem
The easiest way would probably be to have a private method that sets the ToolTip correctly and then call that method from each setter involved. If any of the properties involved get changed just update your ToolTip.
Stephan
Thanks, Stephan. I havn't yet tried your last proposal, but your remark on the fact that there is no reason to handle **MouseEnter** event have led me to what seems to work OK now - I put the **ToolTipService.SetToolTip** inside the **OnApplyTemplate()** and ToolTips are now working stable. +1
rem