我有一個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代碼嗎? – kennyzx 2015-03-31 05:27:08
嗨Truong,你有沒有試過我的答案?對你起作用嗎? – kennyzx 2015-03-31 10:06:57
hi kennyzx。我已經試過了你的答案和我的功能相同的結果(在我的帖子後面的代碼)。但是,如果我改變從網格到畫布的根控制。它的位置更接近。感謝 – 2015-04-01 07:29:55