user2495173
user2495173

Reputation: 309

Dynamic tooltip in WPF

I am just few days into WPF and kinda figuring out stuff. I have implemented a Tab and now I need to dynamically populate thee tooltip for the file name. The tab title should only display the file name, were as tooltip should show the entire file path. How do I get this done? The code is as follows:

<!-- XAML -->
<Label Content="TabItem"  Height="23" HorizontalAlignment="Left"
       Margin="4,1,0,0" Name="TabTitle" VerticalAlignment="Top"
       FontFamily="Courier" FontSize="12" ToolTip="Dynamic FilePath"/>

public string Title
{
    set
    {
        ((CloseableHeader)this.Header).TabTitle.Content = ExtractFileName(value);
    }
}

Upvotes: 2

Views: 7403

Answers (2)

Pop
Pop

Reputation: 71

You can just new up a control, and then set the tooltip to this newcontrol

    var but = new Button();         
    // old code 
    but.ToolTip = "some string";
    // new code with font that can be controlled 
    var toolTipTextBox = new TextBox();
    toolTipTextBox.Text = "some string";
    toolTipTextBox.FontSize = 24;
    but.ToolTip = toolTipTextBox;

Upvotes: 3

Grant Winney
Grant Winney

Reputation: 66489

You said you want a ToolTip on a tab, but your XAML is for a label so I'll just use that. Feel free to leave a comment below clarifying what you're doing.

Since you're just using code-behind, give the label a name:

<Label Content="TabItem"  Height="23" HorizontalAlignment="Left"
       Margin="4,1,0,0" Name="TabTitle" VerticalAlignment="Top"
       FontFamily="Courier" FontSize="12" ToolTip="Dynamic FilePath"
       Name="MyLabel" />

And then set the "ToolTip" in the code-behind:

MyLabel.ToolTip = Title;  // or whatever you want to display

Side note:

One of the nice things about WPF is the advanced data-binding built into it. You should look into the MVVM pattern, which allows you to separate the logic (in the ViewModel) from the layout (in the XAML).

Then you could have a property in your ViewModel like "Title", bind your view to the ViewModel, and just set the ToolTip with something like:

<Label ToolTip={Binding Path=Title} ... />

Upvotes: 3

Related Questions