Chris
Chris

Reputation: 27384

Magnifing glass in WPF

How can I create a non-circular magnifying glass in WPF? This has to work on controls not just an image. Every example I find online is either circular only or only works on images.

For example I have a slider, and I'd like to turn the thumb into a rectangular magnifying region to show enlarged ticks (as my ticks are displayed in the Slider track itself, not below it). I have created all the styles necessary I am just missing the ability to magnify contents underneath the thumb (as the thumb sits on top of the controls / display)

<Slider Ticks="{Binding MyCollection}" />

Thanks

Upvotes: 1

Views: 2858

Answers (1)

Sheridan
Sheridan

Reputation: 69979

It's pretty easy to just make your own 'magnifying' control. You could use a VisualBrush with a Visual property taken from the source (that you want to magnify) painted onto a plain Rectangle. See the Using VisualBrush to Show a Magnifying Glass page on the Ian G on Tap website as an example.

Better yet, here is a very simple example of a VisualBrush that is painting a Rectangle in the right column of a Grid, magnifying an Image from the left column of a Grid. You can tweak it to your liking:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Image Name="Image" Source="Images/BlackLogo.ico" Width="150" Height="150" />
    <Rectangle Grid.Column="1">
        <Rectangle.Fill>
            <VisualBrush Visual="{Binding ., ElementName=Image}" 
                Viewport="50,100,300,300" ViewportUnits="Absolute" />
        </Rectangle.Fill>
    </Rectangle>
</Grid>

enter image description here

Upvotes: 3

Related Questions