【发布时间】:2015-06-04 07:47:04
【问题描述】:
我有一个包含图像的 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;
谁能帮帮我! 谢谢
这是我的 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>
【问题讨论】:
-
什么是
ContentPanel?它是画布吗?能否为ContentPanel和OrginalImage添加XAML 代码? -
嗨 Truong,你试过我的回答吗?对你有用吗?
-
嗨,肯尼兹。我已经用我的函数尝试了你的答案和相同的结果(我的帖子后面的代码)。但是,如果我将根控件从 Grid 更改为 Canvas。它的位置更近了。谢谢
标签: image windows-phone-8 textbox