WPF 绑定数据自动生成按钮
程序员文章站
2022-06-07 17:59:45
...
开发工具与关键技术: Visual Studio 2017、C#语言、WPF、SQL Server
作者:邓李庆
撰写时间: 2019年9月18日
下面给大家讲一个WPF中的绑定SQL Server 数据在后端生成按钮。 先在布局上给一个框的面给Name 名。见图代码:
<UserControl x:Class="Multihospital.View.Homemenu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Height="650" Width="1200" Background="White" Loaded="UserControl_Loaded">
<Grid>
<GroupBox Name="Foodcourt" Header="餐桌台" Foreground="Black" Margin="10,10,10,50">
<Grid>
<WrapPanel Name="Seat" Margin="30,10">
<!--座位信息-->
</WrapPanel>
</Grid>
</GroupBox>
</Grid>
</UserControl>
然后给一个 Loaded 加载事件。在里面进行查询SQL Server 里面的数据查询,查询完后使用For 循环进行循环每一条数据,在循环里面声明Button 按钮,给宽和高、字体颜色、还有鼠标左键、右键事件。
//自动加载
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
try
{
int Reserveds = 0, Leisures = 0, Tourists = 0, CleanRooms = 0, Inactives = 0;
//每次都清空WrapPanel:环绕面板的内容
Seat.Children.Clear();
var mySeat = (from dtSeat in myModels.PW_Seat
join dtGenreup in myModels.PW_Genreup on dtSeat.GenreupID equals dtGenreup.GenreupID
join dtEmployee in myModels.PW_Employee on dtSeat.EmployeeID equals dtEmployee.EmployeeID
select new
{
dtSeat.SeatName,
dtSeat.Platform,
dtSeat.NumberPeople,
dtSeat.GenreupID,
dtGenreup.GenreupName,
dtEmployee.EmployeeName,
}).ToList();
for (int i = 0; i < mySeat.Count; i++)
{
Button button = new Button();
button.Height = 100;
button.Width = 100;
button.Margin = new Thickness(5);
button.Foreground = new SolidColorBrush(Colors.White);
button.BorderBrush = new SolidColorBrush(Colors.Transparent);
button.Click += new RoutedEventHandler(btn_Click);//鼠标左键
button.MouseRightButtonUp += new MouseButtonEventHandler(btn_MouseRightButtonDown);//鼠标右键
if (mySeat[i].GenreupID == 1)//预订
{
button.Content = mySeat[i].Platform;
button.ToolTip = "★状 态:" + mySeat[i].GenreupName + "\n★容纳数:" + mySeat[i].NumberPeople + "\n★服务员:" + mySeat[i].EmployeeName;
button.Background = new SolidColorBrush(Colors.Blue);
Reserveds++;
}
else if (mySeat[i].GenreupID == 2)//空闲
{
button.Content = mySeat[i].Platform;
button.ToolTip = "★状 态:" + mySeat[i].GenreupName + "\n★容纳数:" + mySeat[i].NumberPeople + "\n★服务员:" + mySeat[i].EmployeeName;
button.Background = new SolidColorBrush(Colors.Green);
Leisures++;
}
else if (mySeat[i].GenreupID == 3)//使用
{
button.Content = mySeat[i].Platform + "\n\n" + mySeat[i].SeatName;
button.ToolTip = "★状 态:" + mySeat[i].GenreupName + "\n★容纳数:" + mySeat[i].NumberPeople + "\n★服务员:" + mySeat[i].EmployeeName;
button.Background = new SolidColorBrush(Colors.Orange);
Tourists++;
}
else if (mySeat[i].GenreupID == 4)//清理
{
button.Content = mySeat[i].Platform + "\n\n" + mySeat[i].SeatName;
button.ToolTip = "★状 态:" + mySeat[i].GenreupName + "\n★容纳数:" + mySeat[i].NumberPeople + "\n★服务员:" + mySeat[i].EmployeeName;
button.Background = new SolidColorBrush(Colors.Gray);
CleanRooms++;
}
else if (mySeat[i].GenreupID == 5)//停用
{
button.Content = mySeat[i].Platform + "\n\n" + mySeat[i].SeatName;
button.ToolTip = "★状 态:" + mySeat[i].GenreupName + "\n★容纳数:" + mySeat[i].NumberPeople + "\n★服务员:" + mySeat[i].EmployeeName;
button.Background = new SolidColorBrush(Colors.Red);
Inactives++;
}
Reserved.Text = Reserveds + "间";
Leisure.Text = Leisures + "间";
Tourist.Text = Tourists + "间";
CleanRoom.Text = CleanRooms + "间";
Inactive.Text = Inactives + "间";
//给WrapPanel:环绕面板添加子集
Seat.Children.Add(button);
}
}
catch (Exception)
{
MessageBox.Show("计算机网络连接失败!", "提示", MessageBoxButton.OK, MessageBoxImage.Stop);
}
}
进行判断鼠标左键还是右键
//鼠标单击键
private void btn_Click(object sender, RoutedEventArgs e)
{
}
//鼠标右键
private void btn_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
}
上一篇: 日期转换为星期