Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
808 views
in Technique[技术] by (71.8m points)

vb.net - Creating a WPF Window that allows zooming and panning

I want to create a Window that will hold several controls. However, I would like the user to be able to pan around and zoom in and out to see larger versions of those controls.

I don't even know where to begin looking.

I was going to start at ScaleTransform that responds to the use of the scroll button on the mouse but I am not sure if that is the best idea.

Just need a push in the right direction.

thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This might be a good candidate for a Viewbox.

See here: http://msdn.microsoft.com/en-us/library/system.windows.controls.viewbox(v=vs.110).aspx

Basically, you can Wrap the entire contents of the Window into a Viewbox like so:

<Window>
   <Viewbox>
       <!-- content here -->
   </Viewbox>
</Window>

and then bind to the Viewbox control's width and height to simulate the zooming. For a quick test, you could just listen to scroll wheel events via code-behind, name the Viewbox control, and access the Viewbox directly at change the values there.

Edit: here's a scenario I just found to get you started. They are using an image, but it's the exact same concept that I described above.

http://www.c-sharpcorner.com/uploadfile/yougerthen/working-with-wpf-viewbox-control/

Edit2: Quick working example using mouse-scroll

Xaml:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        MouseWheel="MainWindow_OnMouseWheel">
    <Grid>
        <Viewbox x:Name="ZoomViewbox" Stretch="Fill">
            <StackPanel>
                <Label Content="Label" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                <Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" />
            </StackPanel>
        </Viewbox>
    </Grid>
</Window>

C#:

using System.Windows;
using System.Windows.Input;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            ZoomViewbox.Width = 100;
            ZoomViewbox.Height = 100;
        }

        private void MainWindow_OnMouseWheel(object sender, MouseWheelEventArgs e)
        {
            UpdateViewBox((e.Delta > 0) ? 5 : -5);
        }

        private void UpdateViewBox(int newValue)
        {
            if ((ZoomViewbox.Width >= 0) && ZoomViewbox.Height >= 0)
            {
                ZoomViewbox.Width += newValue;
                ZoomViewbox.Height += newValue;   
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...