Input dialogue popup on mouse click

梦想的初衷 提交于 2019-12-13 04:59:31

问题


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.

Here's my code at the moment:

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>

Code behind:

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);

        } 

    }
}

回答1:


Did you try using the PopUp window?

XAML:

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

in your code behind:

errMsg.IsOpen = true;

Or you could either create fully using your code:

WPF popup window

Reference:

Popup a User Control



来源:https://stackoverflow.com/questions/27111814/input-dialogue-popup-on-mouse-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!