鼠标单击时弹出输入对话框 [英] Input dialogue popup on mouse click

查看:36
本文介绍了鼠标单击时弹出输入对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做到这一点,当用户单击我画布中的某个位置时,会显示一个弹出窗口,允许用户输入两个单独的数据.所以我需要两个文本块,然后是一种将输入的数据保存到后面代码中的一些变量中的方法.我一直在看不同的教程,用输入 texblocks 制作一个窗口很容易.只是不确定如何使用弹出窗口来做到这一点.addNode_MouseDown 方法是我尝试添加弹出窗口的地方,因为输入的信息将与用户在画布上单击时制作的圆圈有关.任何帮助将不胜感激.

I'm trying to make it so when the user clicks on a spot in my canvas, a popup is displayed that allows the user to enter two separate pieces of data. So I need two textblocks and then a way to save the data entered into some variables in the code behind. I've been looking at different tutorials and it's easy making a window with input texblocks. Just not sure how to do it with popups. The addNode_MouseDown method is where I tried adding the popup, since the information entered is going to be about the circle that the user makes when they click on the canvas. Any help would be appreciated.

这是我目前的代码:

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 窗口?

Did you try using the PopUp window?

XAML:

<Popup Name="errMsg" StaysOpen="False">
<TextBox/>
</Popup>

在你后面的代码中:

errMsg.IsOpen = true;

或者您可以完全使用您的代码创建:

Or you could either create fully using your code:

WPF 弹出窗口

参考:

弹出用户控件

这篇关于鼠标单击时弹出输入对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆