我试图绑定ListBoxItem的工具提示。我在ControlTemplate中定义了这个:
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<ControlTemplate.Resources>
<conv:IconConverter x:Key="IconConverter" />
<conv:FirstImageConverter x:Key="FirstImageConverter"/>
<conv:DebugConverter x:Key="dbg"/>
</ControlTemplate.Resources>
<Border ...>
<StackPanel>
<Image MaxHeight="160" Stretch="UniformToFill" Source="{Binding Icon,Converter={StaticResource IconConverter},ConverterParameter=128}" Height="128"/>
<TextBlock x:Name="lblName" Text="{Binding Name}" />
</StackPanel>
<Border.ToolTip>
<DockPanel LastChildFill="True" Margin="2" MaxWidth="800"
DataContext="{Binding Images,Converter={StaticResource FirstImageConverter}}"
IsEnabled="{Binding Images,Converter={StaticResource FirstImageConverter},ConverterParameter=enabled}">
<TextBlock DockPanel.Dock="Bottom" Width="Auto" MaxWidth="600" Text="{Binding Caption}" Height="Auto" />
<Image x:Name="imgFullSize" DockPanel.Dock="Top" Stretch="None" Width="Auto" Source="{Binding Filename,Converter={StaticResource IconConverter}}"/>
</DockPanel>
</Border.ToolTip>
是的,我收到了绑定错误:System.Windows.Data Error: 3 : Cannot find element that provides DataContext. BindingExpression:Path=Images; DataItem=null; target element is 'DockPanel' (Name=''); target property is 'DataContext' (type 'Object')
我的ListBoxItem的datacontext确实包含images属性,因为它具有正确显示的名称和图标属性。我还尝试使用TooltipService.ToolTip
代替Border.ToolTip
,但没有效果。有什么区别,为什么我的装订不起作用?
答案 0 :(得分:3)
最终通过将工具提示更改为:
来解决此问题...
<Border.ToolTip>
<ToolTip DataContext="{Binding PlacementTarget.DataContext.Images, RelativeSource={RelativeSource Self}}" Visibility="{Binding PlacementTarget.DataContext.Images, RelativeSource={RelativeSource Self},Converter={StaticResource FirstImageConverter},ConverterParameter=visible}">
<DockPanel LastChildFill="True" Margin="2" MaxWidth="800">
...
感谢@Rachel带领我回答。