如何获得点击位置来添加控制?

时间:2015-03-31 04:38:09

标签: image windows-phone-8 textbox

我有一个带有图片的 Windows phone 8.0 项目。当用户点击图像时,我必须获得点击的位置并在此位置添加文本框(上图)。

目前,我使用以下代码,但总是出错位置

        MessageBox.Show("vao day");
        var mp = GetMousePoint(e);
        MessageBox.Show("x=" + mp.X + "_" + "y=" + mp.Y);
        //x: 258; y:97
        double top= 0; double right = 0; double bottom =0; double left=0;
        top = e.GetPosition(OrginalImage).Y; //(double)mp.Y - 50;
        left = e.GetPosition(OrginalImage).X; //(double)mp.X - 50;

        MessageBox.Show("x=" + left.ToString() + "_" + "y=" + top.ToString());
        //add textbox to this position
        TextBox newtext = new TextBox();
        newtext.Text = "truongpm";
        newtext.Margin = new Thickness(left, top, right, bottom);
        newtext.Width = 124;
        newtext.Height = 68;

        //button.VerticalAlignment = 234;
        //Add contentpanel to your page if there's not one already.
        ContentPanel.Children.Add(newtext);
        newtext.Visibility = System.Windows.Visibility.Visible; 

enter image description here

是的,请有人帮帮我! 感谢

这是我的xaml代码

    <Grid x:Name="ContentPanel" Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Image Grid.Row="0" MouseMove="Image_MouseMove" Name="OrginalImage" Stretch="Fill" Height="250" Source="/Assets/vd1.png"/>
        <Button Grid.Row="1" Content="ALter" Width="100" Height="80" Click="AlterButton_Click" Foreground="#FFD66A6A" BorderBrush="#FF16C71E" Margin="10,0,360,0"/>
        <Button Grid.Row="1" Content="Save" Width="100" Height="80" Foreground="#FFD66A6A" BorderBrush="#FF16C71E" Margin="115,0,255,0" Click="Button_Click"></Button>
        <Button Grid.Row="1" Content="Text" Width="100" Height="80" Foreground="#FFD66A6A" BorderBrush="#FF16C71E" Margin="115,0,055,0" Click="TextButton_Click"></Button>
        <Image Grid.Row="2" Name="AlterImage" />

        <!--<TextBox HorizontalAlignment="Left" Height="72" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="180" Margin="110,65,0,0" Grid.Row="0"/>-->
    </Grid>

</Grid>

2 个答案:

答案 0 :(得分:0)

获得职位

void Touch_FrameReported(object sender, TouchFrameEventArgs e) 
        { 
            TouchPoint touchPoint = e.GetTouchPoints(this.ContentPanel).FirstOrDefault(); 
            if (touchPoint.Action == TouchAction.Up) 
            MessageBox.Show("Selected coordinates:"+touchPoint.Position.X + "," + touchPoint.Position.Y);//Displaying x&y co-ordinates of Touch point 
        } 

来源于:This Link ...

答案 1 :(得分:0)

问题是默认情况下TextBox位于ContentPanel(网格)的中心。你希望它位于左上角,对吧?如果ContentPanel是Canvas,而不是Grid,那就是这样。

您可以通过将TextBox's Margin设置为(0,0,0,0)来验证这一点,请注意TextBox将位于中心,而不是位于左上角。

double top= 0; double right = 0; double bottom =0; double left=0;
newtext.Margin = new Thickness(left, top, right, bottom); 

现在修复:设置Horizo​​ntalAlignment和VerticalAlignment属性。

TextBox newtext = new TextBox(); 
newtext.HorizontalAlignment = HorizontalAlignment.Left;
newtext.VerticalAlignment = VerticalAlignment.Top;