0

I am trying to use canvas inside windows form and zoom and pan that canvas for that first i put element host and then put canvas inside it and then put picture box in canvas and then trying to zoom canvas i have tried various ways but event of any control does not executed i have also written all mouse wheel events but no one is executed so please suggest me solution below is my code for adding controls and mouse wheel events

         elementHost1.Height = picVarify.Height;
        elementHost1.Width = picVarify.Width;
        elementHost1.Location = picVarify.Location;
        touchcanvas = new System.Windows.Controls.Canvas();
        WindowsFormsHost hst = new WindowsFormsHost();
        hst.Name = "Host";
        hst.Child = picVarify;
        hst.Height = picVarify.Height;
        hst.Width = picVarify.Width;
        touchcanvas.Height = picVarify.Height;
        touchcanvas.Width = picVarify.Width;
        touchcanvas.Children.Add(hst);
        zm = new ZoomAndPan.ZoomAndPanControl();
        zm.Name = "zm";
        zm.Content = touchcanvas;
        zm.MouseWheel += new System.Windows.Input.MouseWheelEventHandler(zoomAndPanControl_MouseWheel);
        elementHost1.Child = zm;
        touchcanvas.MouseWheel += new System.Windows.Input.MouseWheelEventHandler(touchcanvas_MouseWheel);
        hst.MouseWheel += new System.Windows.Input.MouseWheelEventHandler(hst_MouseWheel);
        picVarify.MouseWheel += new MouseEventHandler(picverify_MouseWheel);
user3269550
  • 452
  • 4
  • 15

1 Answers1

0

here is the code that's working fine for me. I added canvas inside a canvas and set background property of both canvases.

XAML

<Window x:Class="WpfStack.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid x:Name="rootGrid" Margin="10,0,0.4,3.8">
    <Canvas Name="BaseCanvas" Background="AliceBlue" Margin="10,0,0,10">

    </Canvas>
</Grid>

Code

     public MainWindow()
        {
            InitializeComponent();
            Canvas touchcanvas = new System.Windows.Controls.Canvas();
            touchcanvas.Height =100;
            touchcanvas.Width = 100;
            touchcanvas.Background = Brushes.Transparent;
            touchcanvas.MouseWheel += new System.Windows.Input.MouseWheelEventHandler(touchcanvas_MouseWheel);
            BaseCanvas.Children.Add(touchcanvas);
            BaseCanvas.Background = Brushes.Transparent;

        }

     public void touchcanvas_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs args)
        {
            args.Handled = true;

        }
Abdul Rehman
  • 415
  • 5
  • 16