栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

WPF 绑定数据自动生成按钮

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

WPF 绑定数据自动生成按钮

开发工具与关键技术: Visual Studio 2017、C#语言、WPF、SQL Server
作者:邓李庆
撰写时间: 2019年9月18日
下面给大家讲一个WPF中的绑定SQL Server 数据在后端生成按钮。 先在布局上给一个框的面给Name 名。见图代码:


 
        
            
                
                    
                
            
        
    
    

然后给一个 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 + "nn" + 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 + "nn" + 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 + "nn" + 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)
        {

        }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/955134.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号