Getting a tooltip in a user control to show databound text and stay open

后端 未结 1 826
小蘑菇
小蘑菇 2020-12-18 14:29

I have a user control that shows a TextBox along with a small help icon.

My goal is to have a ToolTip pop-up, show some databound text and

1条回答
  •  渐次进展
    2020-12-18 14:59

    ToolTips are not part of the same VisualTree as the rest of your XAML, so the DataContext is not inherited the way you would expect it to be.

    Writing ToolTip="{Binding SomeProperty}" will automatically set the ToolTip's DataContext to SomeProperty, however if you build a custom ToolTip you must do this yourself.

    
    

    This will bind the ToolTip's DataContext to the DataContext of whatever object the ToolTip is on.

    To accomplish what you're trying to do, your would probably look like this, since PlacementTarget would be your Image:

    
    
        
            
        
    
    

    As for why it won't stay open, I'm not positive but it might be because the ToolTipService.ShowDuration property defaults to 5 seconds, and that probably overwrites the StaysOpen property.

    You can try setting it to something higher, such as

    
    

    Or you can try this workaround of using a Popup styled to look like a ToolTip instead. The code would probably look something like this:

    
        
    
    

    0 讨论(0)
提交回复
热议问题