我在这里有一个类似的问题: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;
}
}
}
答案 0 :(得分:0)
为了澄清,您是否说您希望弹出框显示为用户可以移动的完全独立的窗口?
如果这是您的第一个WPF应用程序,很难知道要走多远。但是在我看来,你正在闯入MVVM和数据绑定对你有很大帮助的领域。
我强烈建议您花一些时间来掌握MVVM pattern和WPF的数据绑定范例。那里有很多很好的教程。
一旦掌握了基础知识,我强烈推荐使用MVVM-Light Toolkit来减轻创建ViewModel和ICommands的痛苦。它还包含一个baisc消息服务,简单地在Windows和Views之间进行通信。
抱歉,我实际上没有回答你的问题 - 但希望这些链接会给你一些帮助:)