【问题标题】:Popup window to get user input弹出窗口以获取用户输入
【发布时间】:2014-12-01 16:04:23
【问题描述】:

我在这里也有类似的问题:Input dialogue popup on mouse click

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

我尝试在弹出窗口后添加一个标签,但我得到一个 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;

    } 

}
}

【问题讨论】:

    标签: c# wpf xaml window user-input


    【解决方案1】:

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

    如果这是您的第一个 WPF 应用程序,很难知道要走多远。但在我看来,您似乎误入了 MVVM 和数据绑定对您有很大帮助的领域。

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

    一旦您掌握了基础知识,我强烈推荐MVVM-Light Toolkit 以减轻创建 ViewModel 和 ICommand 的痛苦。它还包含一个 baisc 消息服务,用于简单地在 Windows 和 Views 之间进行通信。

    抱歉,我还没有真正回答你的问题 - 但希望这些链接能给你一些帮助:)

    【讨论】:

    • 是的,没错。现在,当用户单击画布上的某个位置时,它只是一个带有文本框和标签的空白区域。感谢您的链接,我会读一读!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多