WPF弹窗示例:点击按钮显示详细信息并填写内容
在WPF中,可以使用弹窗(Window)来显示详细信息并进行填写。以下是一个简单的示例代码:
首先,在XAML文件中创建一个按钮和一个弹窗:
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button Content="填写详细信息" Click="Button_Click"/>
</Grid>
</Window>
接下来,在代码文件中实现点击按钮时显示弹窗的逻辑:
using System.Windows;
namespace WpfApp
{
public partial class MainWindow : Window
{
private DetailWindow detailWindow;
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (detailWindow == null)
{
detailWindow = new DetailWindow();
detailWindow.Closed += (s, args) => detailWindow = null;
detailWindow.Show();
}
else
{
detailWindow.Activate();
}
}
}
}
最后,在代码文件中创建弹窗的XAML和代码逻辑:
<Window x:Class="WpfApp.DetailWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DetailWindow" Height="300" Width="400">
<Grid>
<TextBox Name="textBox" Margin="10"/>
<Button Content="保存" HorizontalAlignment="Right" Margin="10" Click="Button_Click"/>
</Grid>
</Window>
using System.Windows;
namespace WpfApp
{
public partial class DetailWindow : Window
{
public DetailWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// 保存详细信息的逻辑
string detail = textBox.Text;
// ...
Close();
}
}
}
这样,当点击'填写详细信息'按钮时,会弹出一个弹窗,其中包含一个文本框和一个保存按钮。用户可以在文本框中填写详细信息,并点击保存按钮保存。保存按钮的点击事件中可以添加保存详细信息的逻辑。
原文地址: https://www.cveoy.top/t/topic/BqG 著作权归作者所有。请勿转载和采集!