如何在画布内获得矩形左上角的位置. [英] How to Get Rectangle Top Left Position inside the Canvas.

查看:90
本文介绍了如何在画布内获得矩形左上角的位置.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在Silverlight应用程序上工作,此应用程序基本上是图像编辑,所以我有Canvas,在画布内我有两个孩子,一个是Image,第二个是Rectange,所以我平移图像,Canvas是Fix size,所以我将如何获得矩形左上角"位置.

我曾尝试过Canvas.GetTop(myRectangle);但这会给我一些错误的值,并且Canvas.GetLet(myRectangle);它也给了我相同的价值,所以我该如何解决这个问题.. !!



我的XAML就是这样..

Hello Guys I am working on silverlight application this application is Basically image edittin so I have Canvas and inside the canvas I have a two child one is Image and Second is Rectange so I am Panning the Image and my Canvas is Fix size so how i''ll get the Rectangle Top Left Position.

I had tried with Canvas.GetTop(myRectangle); but this will give me the some wrong value and Canvas.GetLet(myRectangle); it is also give me the same value so how I can resolve this problem..!!



My XAML is like this..

<canvas>
     x:Name="myCanvas"
     Grid.Row="1"
      ScrollViewer.HorizontalScrollBarVisibility="Visible"
      ScrollViewer.VerticalScrollBarVisibility="Visible"
     Grid.Column="0"
     Width="{Binding ElementName=imgEdit,Path=ActualWidth}"
     Height="{Binding ElementName=imgEdit,Path=ActualHeight}"
     Grid.ColumnSpan="2"
     HorizontalAlignment="Center"
     VerticalAlignment="Center">


     <!--Width="490"
             Height="490"-->

     <Image x:Name="imgEdit"
             Cursor="Hand"
             Stretch="Uniform"
             MouseLeftButtonDown="imgEdit_MouseLeftButtonDown"
             MouseMove="imgEdit_MouseMove"
             MouseLeftButtonUp="imgEdit_MouseLeftButtonUp"
             HorizontalAlignment="Center"
             VerticalAlignment="Center"
             RenderTransformOrigin="0.5, 0.5"
             >

             <Image.Effect>
                 <l:briconeffect xmlns:l="#unknown">
                         Brightness="{Binding ElementName=bVal, Path=Value}"
                         Contrast="{Binding ElementName=cVal, Path=Value}"
                         Gamma="{Binding ElementName=gVal,Path=Value}"
                         RedRatio="{Binding ElementName=rVal,Path=Value}"
                         BlueRatio="{Binding ElementName=blueVal,Path=Value}"
                         GreenRatio="{Binding ElementName=greenVal,Path=Value}"/>
             </Image.Effect>

             <Image.RenderTransform>

                 <transformgroup>
                     <rotatetransform x:name="Rotator" xmlns:x="#unknown">
                         <rotatetransform.angle>
                             <binding elementname="sldVerHorizontal" path="Value" mode="TwoWay" />
                         </rotatetransform.angle>
                     </rotatetransform>

                     <scaletransform x:name="Scale" xmlns:x="#unknown">
                         <scaletransform.scalex>
                             <binding elementname="sldZoomInOut" path="Value" mode="TwoWay" />
                         </scaletransform.scalex>
                         <scaletransform.scaley>
                             <binding elementname="sldZoomInOut" path="Value" mode="TwoWay" />
                         </scaletransform.scaley>
                     </scaletransform>
                 </transformgroup>
             </Image.RenderTransform>
         </Image>

     <border>
             Grid.Row="1"
             Grid.ColumnSpan="2"
             x:Name="maskBorder"
             BorderBrush="Green"
             BorderThickness="5"
             Width="200"
             Height="200"
             Cursor="Hand"
             HorizontalAlignment="Center"
             VerticalAlignment="Center"
             >
                 <!--Canvas.Top="150"
                 Canvas.Left="150"-->

         <rectangle>
                 Grid.Row="1"
                 Grid.ColumnSpan="2"
                 x:Name="maskRect"
                 Width="200"
                 Height="200"
                 HorizontalAlignment="Center"
                 VerticalAlignment="Center"
                 Cursor="Hand"
                 >
         </rectangle>
     </border>

 </l:briconeffect></canvas>

推荐答案

您好,

以下代码可以完美运行:

一个非常简单的Silverlight应用程序,具有一个Canvas,两个子对象,一个图像和一个矩形.

这是Xaml:

Hello,

The following code works perfectly:

A very simple Silverlight application with a Canvas, two children, an image and a rectangle.

This is the Xaml:

<UserControl x:Class="SilverlightApplication2.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"

    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <Canvas Height="163" HorizontalAlignment="Left" Margin="54,69,0,0" Name="canvas1" VerticalAlignment="Top" Width="286">
            <Image Canvas.Left="30" Canvas.Top="25" Height="91" Name="image1" Stretch="Fill" Width="116" Source="/SilverlightApplication2;component/Images/diplodocus2.jpg" />
            <Rectangle Canvas.Left="186" Canvas.Top="68" Height="69" Name="rectangle1" Stroke="Black" StrokeThickness="1" Width="94" />
        </Canvas>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="208,259,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</UserControl>



答:这是背后的代码:



Ans this is the code behind:

private void button1_Click(object sender, RoutedEventArgs e)
{
    foreach (var item in this.canvas1.Children)
    {
        double X = Canvas.GetLeft((UIElement)item);
    }
}



当代码执行时,Canvas.GetLeft返回正确的值.

希望对您有所帮助.

瓦莱里.



When the code executes Canvas.GetLeft returns the correct values.

Hope it helps.

Valery.


这篇关于如何在画布内获得矩形左上角的位置.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆