binncheol
binncheol

Reputation: 1453

How to get ToolTip binding to work with a ComboBox?

Currently I have a ComboBox defined as:

<ComboBox Name="comboItems" ItemsSource="{Binding Path=EnumDataItems}"
            DisplayMemberPath="Description" 
            ToolTip="{Binding Path=ToolTip}" // never displays the value
            SelectedValuePath="Value" SelectedValue="{Binding Path=Value}" />

Everything works except the ToolTip. The property that it should bind to; ToolTip does contain a value. I'm sure of this because when I do the following, I see a result confirming that ToolTip contains a value:

<ComboBox Name="comboItems" ItemsSource="{Binding Path=EnumDataItems}" 
            DisplayMemberPath="ToolTip" // I replaced 'Description' with 'ToolTip'
            ToolTip="{Binding Path=ToolTip}"
            SelectedValuePath="Value" SelectedValue="{Binding Path=Value}"/>

Having replaced Description with ToolTip I can see that the value of ToolTip is appearing on the screen. However

ToolTip="{Binding Path=ToolTip}"

still doesn't work. If I attempt to display ToolTip as follows:

ToolTip="ToolTip" 

it just displays the word 'ToolTip'.

How can I get ToolTip to display a value?

Upvotes: 21

Views: 21890

Answers (2)

LPL
LPL

Reputation: 17063

If a ToolTip for every ComboBoxItem is what you want you can do this:

<ComboBox.ItemContainerStyle>
    <Style>
        <Setter Property="Control.ToolTip" Value="{Binding ToolTip}" />
    </Style>
</ComboBox.ItemContainerStyle>

Upvotes: 41

max
max

Reputation: 34417

ToolTip="{Binding Path=ToolTip}" binds to ToolTip property of current combo box DataContext (object that contains EnumDataItems property). Assuming you want to set ToolTip of ComboBox to currently selected item's ToolTip property value, this should fix the problem:

ToolTip="{Binding Path=SelectedItem.ToolTip, RelativeSource={RelativeSource Self}}"

Upvotes: 28

Related Questions