单击鼠标时输入对话框弹出窗口

时间:2014-11-24 18:36:39

标签: c# wpf xaml user-input

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

这是我目前的代码:

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

        } 

    }
}

1 个答案:

答案 0 :(得分:0)

您是否尝试使用PopUp窗口?

XAML:

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

在您的代码中:

errMsg.IsOpen = true;

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

WPF popup window

参考:

Popup a User Control