2014-12-01 133 views
0

我也有类似的问题在这里:Input dialogue popup on mouse click弹出窗口让用户输入

所以当画布上的用户点击,一个彩色圆圈标记在鼠标点击的地方,并弹出窗口显示有两个文本框获得用户输入。我试图完成两件事:代替弹出窗口,我希望显示一个窗口,以便用户可以移动弹出窗口(现在它只是一个位于同一位置的空白空间)。我也想添加一个'ok'按钮,这样当它被点击时,两个输入被保存到它们各自的变量,窗口​​关闭。

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

    } 

} 
} 

回答

0

为了澄清,你说你更喜欢弹出框显示为完全独立的窗口,用户可以四处移动?

如果这是您的第一个WPF应用程序,很难知道要走多深。但是在我看来,你正在偏离MVVM和Databinding对你的帮助很大的领域。

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

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

对不起,我没有真正回答你的问题 - 但希望链接会给你一些帮助:)

+0

是的,这是正确的。现在,当用户在画布上点击某处时,它只是一个带有文本框和标签的空白区域。感谢您的链接,我会给他们一个阅读! – pfinferno 2014-12-01 16:26:51