我也有類似的問題在這裏:Input dialogue popup on mouse click彈出窗口讓用戶輸入
所以當畫布上的用戶點擊,一個彩色圓圈標記在鼠標點擊的地方,並彈出窗口顯示有兩個文本框獲得用戶輸入。我試圖完成兩件事:代替彈出窗口,我希望顯示一個窗口,以便用戶可以移動彈出窗口(現在它只是一個位於同一位置的空白空間)。我也想添加一個'ok'按鈕,這樣當它被點擊時,兩個輸入被保存到它們各自的變量,窗口關閉。
我試過在彈出窗口後添加一個標籤,但是我得到了一個xamlparseexception。我不知道該怎麼做才能使彈出窗口成爲窗口。關於輸入文本,我見過很多例子,用戶在文本框中輸入文本並將數據保存到變量中,但在文本框窗口關閉後沒有保存它。這是我的第一個wpf應用程序,我正在慢慢嘗試並學習它。下面的代碼我有,因爲它目前爲:
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">
<Popup Name="inputPopup" MouseDown="addNode_MouseDown" >
<Grid Height="150" Background="White" >
<Label Content="Range to object (m): " Height="28" HorizontalAlignment="Left" Margin="39,28,0,0" Name="label1" VerticalAlignment="Top" />
<TextBox x:Name="rangeToObject" Height="23" HorizontalAlignment="Left" Margin="151,30,0,0" VerticalAlignment="Top" Width="120" />
<Label Content="Heading to Object (0-360): " Height="28" HorizontalAlignment="Left" Margin="39,63,0,0" Name="label2" VerticalAlignment="Top" />
<TextBox x:Name="headingToObject" Height="23" HorizontalAlignment="Left" Margin="151,68,0,0" VerticalAlignment="Top" Width="120" />
</Grid>
</Popup>
</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);
inputPopup.IsOpen = true;
}
}
}
是的,這是正確的。現在,當用戶在畫布上點擊某處時,它只是一個帶有文本框和標籤的空白區域。感謝您的鏈接,我會給他們一個閱讀! – pfinferno 2014-12-01 16:26:51