How to get ToolTip binding to work with a ComboBox

2019-02-08 10:27发布

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?

2条回答
闹够了就滚
2楼-- · 2019-02-08 10:57

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}}"
查看更多
该账号已被封号
3楼-- · 2019-02-08 11:19

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>
查看更多
登录 后发表回答