Search code examples
c#wpftooltip

c# wpf tooltip, can't get tooltip text in code behind


I have an xaml datagrid template in my WPF application that binds/pulls data from a sharepoint site. Each row has a button with a tooltip and the text is dynamically loaded from this connection. When a user clicked on the button, it would copy the tooltip text to the Windows ClipBoard. Just recently, my existing code behind stopped working, and can't seem to get that dynamically loaded tooltip text anymore to work with in code. I've ruled out the clipboard as I can manually set the text and it copies to the clipboard and I can still see the dynamically loaded text in the UI so it can't be the connection. Below is how I use to get the text in the click event:

var buttonTemplate = ((Button)sender).ToolTip;
var buttonTTtext = ((TextBlock)buttonTemplate).Text;
System.Windows.Clipboard.SetText(buttonTTtext.ToString());

Here is the xaml I'm using in the datagrid template:

<mui:ModernButton Click="someButton_Click" Tag="{Binding Path=MyTemplate}" >
<mui:ModernButton.ToolTip>
<TextBlock Text="{Binding Path=template}" TextWrapping="Wrap" />
</mui:ModernButton.ToolTip>
</mui:ModernButton>

Solution

  • Thanks for the help...the tooltip text was in the DataContext of the button. I solved my issue with this code:

    Button btn = (Button)sender;
    var dc = btn.DataContext.GetType().GetProperty("template").GetValue(btn.DataContext, null);