如何使ToolTip绑定与ComboBox配合使用? [英] How to get ToolTip binding to work with a ComboBox?

查看:50
本文介绍了如何使ToolTip绑定与ComboBox配合使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有一个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}" />

除工具提示外,所有内容均有效.它应该绑定的属性;工具提示 确实包含一个值.我确信这是因为,当我执行以下操作时,我看到一个确认ToolTip包含值的结果:

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}"/>

ToolTip 替换了 Description ,我可以看到屏幕上出现了ToolTip的值.但是

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" 

它仅显示单词'ToolTip'.

it just displays the word 'ToolTip'.

如何获取 ToolTip 来显示值?

推荐答案

ToolTip ="{Binding Path = ToolTip}" 绑定到当前组合框的 ToolTip 属性 DataContext (包含 EnumDataItems 属性的对象).假设您要将 ComboBox ToolTip 设置为当前选定项的 ToolTip 属性值,这应该可以解决此问题:

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}}"

这篇关于如何使ToolTip绑定与ComboBox配合使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆