Как сделать панорамирование с ViewBox в WPF? Использование изображений и объектов (эллипс) в ViewBox? - PullRequest
0 голосов
/ 03 ноября 2018

Я разрабатываю приложение WPF. У меня есть элемент управления ScrollView внутри сетки. Внутри элемента управления ScrollView есть элемент управления ViewBox с объектом Image и Ellipse. Функция масштабирования работает просто отлично.

У меня проблема с панорамированием. Каждый раз, когда я нажимаю левую кнопку мыши, ViewBox переходит в правый нижний угол. После этого явления я смог сделать панорамирование, но это не правильная операция.

Как я могу решить эту проблему? Я открыт для любых решений.

Заранее спасибо.

MainWindow.xaml

<Window x:Class="WpfTestZoomAndScroll.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"       
        Title="MainWindow" Name="mainwindow" MinHeight="702" MinWidth="1248" Height="768" Width="1366" ResizeMode="CanResizeWithGrip" >

    <Grid x:Name="MasterGrid">

        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />


            <RowDefinition Height="0.1*" />

            <RowDefinition Height="0.5*" />
            <RowDefinition Height="0.5*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />

        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>


            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="0.5*" />
            <ColumnDefinition Width="0.4*" />
            <ColumnDefinition Width="0.5*" />
            <ColumnDefinition Width="0.8*" />
            <ColumnDefinition Width="0.9*" />
            <ColumnDefinition Width="0.4*" />
            <ColumnDefinition Width="0.4*" />
            <ColumnDefinition Width="0.4*" />
            <ColumnDefinition Width="0.5*" />
            <ColumnDefinition Width="0.5*" />


            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>


        <ScrollViewer Name="scrollViewer1" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" Grid.Row="0" Grid.Column="0" Grid.RowSpan="16" Grid.ColumnSpan="11">
            <Viewbox Name="viewbox1">
                <Grid>
                    <Image Name="imgSource" Source="Maps\tes_img.jpg" Height="600" Width="1150"/>
                    <Ellipse Fill="Green" Width="2" Height="2" Canvas.Left="260" Canvas.Top="147" Margin="259,148,889,450"></Ellipse>
                </Grid>
            </Viewbox>
        </ScrollViewer>

        <Button Content="Button" Grid.Row="10" Grid.Column="11"/>



    </Grid>
</Window>

MainWindow.cs

public partial class MainWindow : Window
    {



        private Point origin;
        private Point start;

        public MainWindow()
        {
            InitializeComponent();

            viewbox1.MouseWheel += MainWindow_MouseWheel;
            viewbox1.MouseLeftButtonDown += image_MouseLeftButtonDown;
            viewbox1.MouseLeftButtonUp += image_MouseLeftButtonUp;
            viewbox1.MouseMove += image_MouseMove;

        }



        private void image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (viewbox1.IsMouseCaptured) return;
            viewbox1.CaptureMouse();


            start = e.GetPosition(scrollViewer1);
            origin.X = viewbox1.RenderTransform.Value.OffsetX;
            origin.Y = viewbox1.RenderTransform.Value.OffsetY;


        }

        private void image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            viewbox1.ReleaseMouseCapture();
        }

        private void image_MouseMove(object sender, MouseEventArgs e)
        {
            if (!viewbox1.IsMouseCaptured) return;


            Point p = e.MouseDevice.GetPosition(scrollViewer1);

            Matrix m = viewbox1.RenderTransform.Value;
            m.OffsetX = origin.X + (p.X - start.X);
            m.OffsetY = origin.Y + (p.Y - start.Y);


            viewbox1.RenderTransform = new MatrixTransform(m);

        }


        private void MainWindow_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            Point p = e.MouseDevice.GetPosition(viewbox1);

            Matrix m = viewbox1.RenderTransform.Value;
            if (e.Delta > 0)
                m.ScaleAtPrepend(1.1, 1.1, p.X, p.Y);
            else
                m.ScaleAtPrepend(1 / 1.1, 1 / 1.1, p.X, p.Y);

            viewbox1.RenderTransform = new MatrixTransform(m);


        }




    }
...