0

In the code behind I cannot seem to access the Image (or any child) control of a Tooltip of an Image.

Setup: I have a UserControl with an image that on hover displays a tooltip with a larger version of the same source. The control is added to my View using:

<local:GetFrame x:Name="getframe" />

The image is then defined within the usercontrol as follows:

<StackPanel Orientation="Horizontal" x:Name="stkImage">
<TextBlock Text="Latest Image" Margin="0,0,5,0"/>
<Image Name="lastImageThumbnail" Source="/Content/blank.jpg" Height="30" Width="30" ToolTipService.ShowOnDisabled="True">
    <Image.ToolTip>
        <ToolTip Placement="Bottom">
            <ToolTip.Template>
                <ControlTemplate>
                    <StackPanel>
                        <Path Margin="5,0,0,0" Fill="DarkGray" Data="M 0 16 L 16 0 L 32 16 Z"/>
                        <Image Name="lastImage" Height="400" Width="400" Source="/Content/blank.jpg"/>
                    </StackPanel>
                </ControlTemplate>
            </ToolTip.Template>
        </ToolTip>
    </Image.ToolTip>
</Image>

At compile time both Image controls point to a resource of a 'default' image. At runtime after I trigger some action I want to update the Source for both images to a new image, but I cannot seem to access the child Image control. On this action lastImageThumbnail changes to my runtime BitmapImage.

if (File.Exists(fullpath))
{
    var image = new BitmapImage(new Uri(fullpath));
    getframe.lastImageThumbnail.Source = image;
    getframe.lastImage.Source = image;
}

getframe.lastImage.Source has error S1061 'GetFrame' does not contain a definition for 'lastImage.... by giving the control a unique name I believed in WPF I should be able to access it but this does not appear to be the case.

5
  • It seems "late" but it isn't. If you add a "Tool tip opened" handler, the "sender" will be the Tooltip. Cast sender as Tooltip and you now have access to the Tooptip's content; including it's DataContext (which is what I tend to focus on; i.e. the "selected / current UI object"). Update what you want, exit the handler, and the Tooltip should display just fine. Commented Mar 27 at 14:25
  • Will have to lookup how to do this specifically, and it seems like a viable alternative (including lots of other 'simple' solutions such as giving the thumbnail a click event and spawning a dialog box to show the bigger version). What I'm really asking with this question is why the 'child' Image control is not accessible in the code behind. I am misunderstanding some structural behaviour in WPF Controls that makes this approach impossible (or I have a silly typo of some kind!) Commented Mar 27 at 15:10
  • The x:Name directive generates private fields. You can change that by x:FieldModifier.
    – Clemens
    Commented Mar 27 at 15:29
  • It may however make sense to bind the Source properties to some view model properties. See Data binding overview.
    – Clemens
    Commented Mar 27 at 15:30
  • @Clemens X:FieldName was not valid (MC3073) for any control within ToolTip (you cannot give Image.Tooltip x: field either and I suspect this is whats making the 'child' controls inaccessible?) Adding x:FieldModifier="public" to the top level Image did not make the code behind be able to access the 'child' Image either I agree changing to a bound property is probably the right solution and I'm working on that now but I'm really curious why this behaviour is invalid as it seems to break the other WPF legal behaviour Commented Mar 27 at 15:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.