我试图做到这一点,当用户单击画布中的某个位置时,会显示一个弹出窗口,允许用户输入两个单独的数据片段。所以我需要两个文本块,然后是保存输入到代码隐藏中某些变量中的数据的方法。我一直在看不同的教程,很容易制作一个带有输入 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);
}
}
}
您是否尝试使用PopUp
窗口?
XAML:
<Popup Name="errMsg" StaysOpen="False">
<TextBox/>
</Popup>
在代码隐藏中:
errMsg.IsOpen = true;
或者,您可以完全使用代码创建:
WPF 弹出窗口
参考:
弹出用户控件