WPF绑定到工具提示 [英] WPF Binding to Tooltip
问题描述
不确定在这里做什么,但是绑定仅适用于数据模板中的标签,而不适用于工具提示.任何帮助将不胜感激.
Not sure whats doing here, but the binding works for the label in the data template but not the tool tip. Any help will be appreciated.
<DataTemplate DataType="Label">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<StackPanel.ToolTip>
<ToolTip DataContext="{Binding Path=PlacementTarget,
RelativeSource={x:Static RelativeSource.Self}}">
<TextBlock Text="{Binding Path=DataContext.Description}" />
</ToolTip>
</StackPanel.ToolTip>
<Image Source="{StaticResource ApplicationInfoS}"
Margin="0 0 5 0" Stretch="None"
HorizontalAlignment="Left" />
<Label Style="{StaticResource lblTextContent}"
Padding="5 0 0 0"
Content="{Binding Path=DataContext.Description, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"/>
</StackPanel>
</DataTemplate>
在列表视图中使用DataTemplate.绑定到列表视图的视图模型上存在描述"属性.
BTW the DataTemplate is used in Listview. "Description" property exists on the view model bound to the list view.
我在VS2010的输出窗口中收到的消息是:
The message I get in the output window in VS2010 is:
System.Windows.Data错误:39:BindingExpression路径错误:在'object''String'(HashCode = -466763399)'上找不到'Description'属性. BindingExpression:Path = DataContext.Description; DataItem ='StackPanel'(Name ='');目标元素是'TextBlock'(Name ='');目标属性是文本"(类型为字符串")
System.Windows.Data Error: 39 : BindingExpression path error: 'Description' property not found on 'object' ''String' (HashCode=-466763399)'. BindingExpression:Path=DataContext.Description; DataItem='StackPanel' (Name=''); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
更新
我已经放弃了.暂时使用以下技巧:
I have given up for now. Using the following hack for the time being:
将标签添加到StackPanel并绑定描述"
Add a Tag to the StackPanel and Bind "Description" to it
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Tag="{Binding Path=DataContext.Description, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}">
将工具提示绑定到标签.是的,但是可以使用.
Bind the ToolTip to the Tag. Yes a hack but it works.
<StackPanel.ToolTip>
<ToolTip DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}">
<TextBlock Text="{Binding Path=Tag}" />
</ToolTip>
</StackPanel.ToolTip>
欢呼
迈克
推荐答案
工具提示不需要相对源绑定.在没有数据上下文绑定的情况下尝试此操作
Tooltip should not need a relativesource binding. Try this with no data context binding
<StackPanel.ToolTip>
<ToolTip Content={Binding Description} />
<StackPanel.ToolTip>
这是假定此DataTemplate所基于的Label类型具有称为Description的属性.在某些情况下,您可能需要绑定到PlacementTarget的少数情况之一是使用ContextMenu控件.
This is assuming that the Label type that this DataTemplate is based on has a property called Description. One of the few instances where you may need to bind to the PlacementTarget is in some cases with a ContextMenu control.
这篇关于WPF绑定到工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!