0
我想讓它如此,當用戶點擊我的畫布上的一個點時,顯示一個彈出窗口,允許用戶輸入兩個單獨的數據。所以我需要兩個文本塊,然後將輸入的數據保存到後面代碼中的某些變量中。我一直在尋找不同的教程,並且使用輸入texblocks很容易製作一個窗口。只是不知道如何與彈出窗口。 addNode_MouseDown方法是我嘗試添加彈出窗口的地方,因爲輸入的信息是關於用戶點擊畫布時所作的圓圈。任何幫助,將不勝感激。輸入對話框彈出鼠標點擊
這裏是我的代碼的時刻:
XAML:
<Window x:Class="CanvasStuff.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Main Window" Height="410" Width="869">
<Grid Height="387">
<Label Content="Image" Height="32" HorizontalAlignment="Left" Margin="11,10,0,0"
Name="selectedFileName" VerticalAlignment="Top" Width="137"
Background="LightGray" BorderBrush="Gray" BorderThickness="1"/>
<Button Content="Browse File" Height="34" HorizontalAlignment="Left" Margin="154,6,0,0"
Name="BrowseButton" VerticalAlignment="Top" Width="119"
Foreground="Maroon" FontSize="16" FontFamily="Georgia" Click="BrowseButton_Click" />
<Button Content="Input Range and Heading" Height="34" HorizontalAlignment="Left" Margin="279,6,0,0"
Name="InputRangeBearing" VerticalAlignment="Top" Width="191"
Foreground="Maroon" FontSize="16" FontFamily="Georgia" Click="InputButton_Click" />
<Canvas Margin="0,45,2,8" x:Name="canvas1" MouseDown= "addNode_MouseDown">
</Canvas>
</Grid>
</Window>
後面的代碼:
namespace CanvasStuff
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
private void BrowseButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.InitialDirectory = "c:\\";
dlg.Filter = "Image files (*.jpg)|*.jpg|All Files (*.*)|*.*";
dlg.RestoreDirectory = true;
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string selectedFileName = dlg.FileName;
ImageBrush brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri(selectedFileName));
canvas1.Background = brush;
BitmapImage bitmap = new BitmapImage();
}
}
private void InputButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Please click on known object to enter range and heading of that object.");
}
private void addNode_MouseDown(object sender, MouseButtonEventArgs e)
{
Point currentPoint = new Point();
if (e.ButtonState == MouseButtonState.Pressed)
currentPoint = e.GetPosition(this);
Ellipse ellipse = new Ellipse();
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Color.FromArgb(255, 255, 255, 0);
ellipse.Fill = mySolidColorBrush;
ellipse.Width = 10;
ellipse.Height = 10;
Canvas.SetLeft(ellipse, e.GetPosition(canvas1).X);
Canvas.SetTop(ellipse, e.GetPosition(canvas1).Y);
canvas1.Children.Add(ellipse);
}
}
}
感謝您的回覆。我研究了PopUp,但是我發現人們說使用它來獲取輸入不是很好的做法,只是爲了顯示一些文本。我會嘗試一下。 – pfinferno 2014-11-26 13:17:17
好吧,我可以使用彈出窗口來獲取一個輸入,但不能使用兩個,因爲「只能有一個孩子」錯誤。例如,我可以有一個輸入文本框,但不能在它旁邊有一個標籤。我需要能夠顯示兩個標籤,然後顯示兩個文本框以供輸入。我嘗試了一個新窗口,但同樣的事情。 – pfinferno 2014-12-01 13:53:02