locked
bound image inside canvas

    Question

  • I am dragging an image inside a canvas . I want to bound that image inside canvas . what to do ? here is my code.

    //xaml 
    <Canvas x:Name="canvas1"  Grid.Row="0" Grid.Column="0" Margin="0" Grid.ColumnSpan="2" Grid.RowSpan="2" Background="White" > 
    
         <Image x:Name="image1" Height="100" Width="100" Margin="0" HorizontalAlignment="Left"  ManipulationDelta="Image_ManipulationDelta_1" ManipulationMode="All">
                    <Image.RenderTransform>
                        <CompositeTransform/>
                    </Image.RenderTransform>
                </Image></Canvas>`
     `//C# 
    private void Image_ManipulationDelta_1(object sender, ManipulationDeltaRoutedEventArgs e)
        {
            var imag = (CompositeTransform)image1.RenderTransform;
            imag.TranslateX += e.Delta.Translation.X;
            imag.TranslateY += e.Delta.Translation.Y;}
    Monday, February 17, 2014 5:05 PM

Answers

  • In your ManipulationDelta handle you can limit the values you set on your TranslateTdansform to stay within the bounds.
    Monday, February 17, 2014 6:39 PM
    Owner
  • Hope this code will help :

      CompositeTransform imgTrans = image1.TransformToVisual(canvas1);
       Windows.Foundation.Point imgPoint = imgTrans.TransformPoint(new Windows.Foundation.Point());
     var imag  = (CompositeTransform)image1.RenderTransform;
    			
        if (imgPoint.X <= 0)
        {
           imag .TranslateX -= imgPoint.X + 5;
           imag .TranslateY += e.Delta.Translation.Y;
        }

    Friday, February 21, 2014 1:05 PM

All replies

  • In your ManipulationDelta handle you can limit the values you set on your TranslateTdansform to stay within the bounds.
    Monday, February 17, 2014 6:39 PM
    Owner
  • Can you show me through code ?!
    Monday, February 17, 2014 7:02 PM
  • Hope this code will help :

      CompositeTransform imgTrans = image1.TransformToVisual(canvas1);
       Windows.Foundation.Point imgPoint = imgTrans.TransformPoint(new Windows.Foundation.Point());
     var imag  = (CompositeTransform)image1.RenderTransform;
    			
        if (imgPoint.X <= 0)
        {
           imag .TranslateX -= imgPoint.X + 5;
           imag .TranslateY += e.Delta.Translation.Y;
        }

    Friday, February 21, 2014 1:05 PM