Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DataContext in ToolTip

I'm trying to bind the tooltip of a ListBoxItem. I have this defined in my 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>

Yes I get a binding error: 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')

My ListBoxItem's datacontext does contain the images property as it does the name and icon properties, which are displayed correctly. I've also tried using TooltipService.ToolTip instead of Border.ToolTip, with no effect. What's the difference, and why isn't my binding working?

like image 965
Echilon Avatar asked Nov 15 '25 17:11

Echilon


1 Answers

Eventually solved this by changing the tooltip to this:

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

Thanks @Rachel for leading me to the answer.

like image 165
Echilon Avatar answered Nov 18 '25 07:11

Echilon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!