In WPF, move the view to a specific shape

Aidan Lee 1 Reputation point
2021-03-26T07:06:03.11+00:00

In WPF, I have some 2D shapes in canvas. I need to move the view to a specific shape.

When I click on the shape, the view port automatically centers it and zooms in.

Instead of panning and zooming the canvas with the mouse, I want to automatically move the view.

How do we implement this in C# ?

Any answer would be appreciated.

Aidan

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,669 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,224 questions
{count} votes

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,616 Reputation points
    2021-03-30T02:17:01.513+00:00

    Could you consider popup as a workaround for displaying enlarged shape? Here is my sample for it:

        <Grid>  
            <Rectangle Name="rec" Width="100" Height="100" Fill="Red" VerticalAlignment="Top" MouseLeftButtonDown="rec_MouseLeftButtonDown">  
            </Rectangle>  
            <Popup Name="pop1"  StaysOpen="False" Placement="Center">  
                <Rectangle  Width="{Binding RelativeSource={RelativeSource AncestorType=Window,AncestorLevel=1,Mode=FindAncestor},Path=Width}"  
                            Height="{Binding RelativeSource={RelativeSource AncestorType=Window,AncestorLevel=1,Mode=FindAncestor},Path=Height}"  
                            Fill="Red"  
                            />  
            </Popup>  
        </Grid>  
    

    The rec_MouseLeftButtonDown code is:

    private void rec_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)  
            {  
                pop1.IsOpen = true;  
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments