C# 如何从数据库填充WPF窗口
我想从数据库中自动加载一些数据,以便在加载新窗口时填充一些文本框和组合框 例如,当我单击C# 如何从数据库填充WPF窗口,c#,wpf-controls,auto-populate,C#,Wpf Controls,Auto Populate,我想从数据库中自动加载一些数据,以便在加载新窗口时填充一些文本框和组合框 例如,当我单击window1中的按钮时,window1将显示一个新窗口window2,并将ID传递给window2(查询数据库需要ID)。 我该怎么做 谢谢,请使用 请遵循以下教程 这只是一个非常简单的示例,说明了您可以做什么: main window.xaml <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.mi
window1
中的按钮时,window1将显示一个新窗口window2
,并将ID
传递给window2
(查询数据库需要ID)。
我该怎么做
谢谢,请使用
请遵循以下教程
这只是一个非常简单的示例,说明了您可以做什么: main window.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Name="btn1" Click="btn1_Click" Content="Button" Margin="10,10,361,283"></Button>
</Grid>
</Window>
<Window x:Class="WpfApplication1.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window2" Height="300" Width="300">
<Grid Margin="0,0,170,249">
<TextBox Name="txtBox1" Margin="18,160,-18,-173"></TextBox>
<TextBox Name="txtBox2" Margin="18,119,-18,-134"></TextBox>
<TextBox Name="txtBox3" Margin="18,76,-18,-93"></TextBox>
<TextBox Name="txtBox4" Margin="18,36,-18,-50"></TextBox>
</Grid>
</Window>
Window2.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Name="btn1" Click="btn1_Click" Content="Button" Margin="10,10,361,283"></Button>
</Grid>
</Window>
<Window x:Class="WpfApplication1.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window2" Height="300" Width="300">
<Grid Margin="0,0,170,249">
<TextBox Name="txtBox1" Margin="18,160,-18,-173"></TextBox>
<TextBox Name="txtBox2" Margin="18,119,-18,-134"></TextBox>
<TextBox Name="txtBox3" Margin="18,76,-18,-93"></TextBox>
<TextBox Name="txtBox4" Margin="18,36,-18,-50"></TextBox>
</Grid>
</Window>
1) 在窗口中创建ui元素
2) 创建具有必需字段的模型类
3) 单击
window1中的按钮将id传递给下一个窗口的构造函数
4) 。使用id根据需要查询数据库。获取结果
并将字段绑定到ui元素值
5) 。这些数据库查询和所有内容都可以写入
构造函数或窗口加载事件
您可以使用。无需编写多余的代码来设置ui的值。谢谢您的示例。。。这正是我需要的