用于获取用户输入的弹出窗口

本文关键字:窗口 输入 获取 用户 用于 | 更新日期: 2023-09-27 18:32:34

>我在这里有一个类似的问题:鼠标单击时弹出输入对话框

因此,当用户单击画布时,会在鼠标单击的位置标记一个彩色圆圈,并显示一个带有两个文本框的弹出窗口以获取用户输入。我正在尝试完成两件事:我希望显示一个窗口,以便用户可以移动弹出窗口,而不是弹出窗口(现在它只是一个位于同一位置的空白区域(。我还想添加一个"确定"按钮,以便在单击它时,两个输入将保存到各自的变量中并关闭窗口。

我尝试在弹出窗口后添加标签,但我得到了一个 xamlparse异常。我不确定该怎么做才能使弹出窗口成为一个窗口。关于输入文本,我见过很多示例,其中用户将文本输入文本框并将数据保存到变量中,但在文本框窗口关闭后没有保存它。这是我的第一个 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;
    } 
}
}

用于获取用户输入的弹出窗口

澄清一下,您是说您希望弹出框显示为用户可以移动的完全独立的窗口吗?

如果这是您的第一个 WPF 应用程序,则很难知道要深入到多深。但在我看来,你正在误入MVVM和数据绑定将为你提供很大帮助的领域。

我强烈建议您花一些时间掌握 MVVM 模式和 WPF 的数据绑定范例。那里有很多很好的教程。

一旦你掌握了基础知识,我强烈推荐MVVM-Light Toolkit来减轻创建ViewModels和ICommand的痛苦。它还包含一个baisc消息传递服务,可以简单地在Windows和视图之间进行通信。

抱歉,我实际上还没有回答您的问题 - 但希望这些链接能为您提供一些帮助:)