Arya
Arya

Reputation: 59

How to get image properties in tooltip

I have one task to get image type, dimensions and size in Tooltip. I tried Using this code. I got Image url , Not able to fetch Image property in ToolTip..

 <Image Source="{Binding Path=UriSource}" Stretch="Fill" Width="100" Height="120">
    <Image.ToolTip>
         <ToolTip Content="{Binding}"/>
    </Image.ToolTip>
</Image>

How to get image Dimensions in tooltip WPF?

Upvotes: 1

Views: 160

Answers (2)

Clemens
Clemens

Reputation: 128061

Since your Binding source object seems to be a BitmapSource, you could directly bind to its properties, e.g. its PixelWidth and PixelHeight:

<Image Source="{Binding}" Width="100" Height="120">
    <Image.ToolTip>
        <ToolTip Content="{Binding}">
            <ToolTip.ContentTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding PixelWidth, StringFormat=Width: {0}}"/>
                        <TextBlock Text="{Binding PixelHeight, StringFormat=Height: {0}}"/>
                    </StackPanel>
                </DataTemplate>
            </ToolTip.ContentTemplate>
        </ToolTip>
    </Image.ToolTip>
</Image>

Or shorter:

<Image Source="{Binding}" Width="100" Height="120">
    <Image.ToolTip>
        <StackPanel>
            <TextBlock Text="{Binding PixelWidth, StringFormat=Width: {0}}"/>
            <TextBlock Text="{Binding PixelHeight, StringFormat=Height: {0}}"/>
        </StackPanel>
    </Image.ToolTip>
</Image>

In case the Binding source is not a BitmapSource, but for instance just an image file Uri or a path string, you may bind to the (automatically created) BitmapSource object in the Image's Source property like this:

<Image Source="{Binding}" Width="160" Height="120">
    <Image.ToolTip>
        <ToolTip DataContext="{Binding PlacementTarget.Source,
                                       RelativeSource={RelativeSource Self}}">
            <StackPanel>
                <TextBlock Text="{Binding PixelWidth, StringFormat=Width: {0}}"/>
                <TextBlock Text="{Binding PixelHeight, StringFormat=Height: {0}}"/>
            </StackPanel>
        </ToolTip>
    </Image.ToolTip>
</Image>

Upvotes: 3

Dominic Jonas
Dominic Jonas

Reputation: 5005

If you want to get the properties of the parent control, you have to set the DataContext of the ToolTip:

    <Image Source="face-monkey.png" Width="60">
        <Image.ToolTip>
            <ToolTip DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}">
                <StackPanel Orientation="Horizontal">
                    <Label Content="Width:" FontWeight="Bold"/>
                    <Label Content="{Binding Width}"/>
                </StackPanel>
            </ToolTip>
        </Image.ToolTip>
    </Image>

monkey

Upvotes: 1

Related Questions