限制画布内的图像移动

时间:2014-02-18 12:46:57

标签: c# windows-8

在Windows 8应用程序中,我在画布中拖动图像。我想在画布中绑定该图像。我该怎么办?

这是我的代码:

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;}

1 个答案:

答案 0 :(得分:0)

您已经拥有代码:

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;
    }