/// <summary> /// 动态生成控件 /// </summary> /// <param name="room"></param> public void ToGenerateTheControl(int int_room) { //清空控件 if (WpButton.Children != null) { WpButton.Children.Clear(); } for (int i = 0; i < int_room; i++) { UC_BtDynamic myBtDynamic = new UC_BtDynamic(LRS); myBtDynamic.Name = "btn" + LRS[i].ID_RoomStage; myBtDynamic.TbRoom_num.Text = LRS[i].Number_RoomStage.ToString(); //房台号 myBtDynamic.TbRoom_class.Text = LRS[i].MC_RoomStage.ToString(); //房台名称 //换台刷新 myBtDynamic.pus += new Pus(Push); myBtDynamic.pus -= new Pus(Push); //获取控件内容id myBtDynamic.GetButtons += new GetButton(SetBtuuons); ////换台刷新 //myBtDynamic.pus += new Pus(Push); //myBtDynamic.pus -= new Pus(Push); //获取控件内容id // myBtDynamic.GetButtons += new GetButton(SetBtuuons); if (LRS[i].State_RoomStage.Trim() == "预定") { myBtDynamic.Background = System.Windows.Media.Brushes.LightSkyBlue; } else if (LRS[i].State_RoomStage.Trim() == "已用") { myBtDynamic.Background = System.Windows.Media.Brushes.DodgerBlue; } else if (LRS[i].State_RoomStage.Trim() == "停用") { myBtDynamic.Background = System.Windows.Media.Brushes.LightSlateGray; } WpButton.Children.Add(myBtDynamic); } }
/// <summary> /// 页面加载事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void UserControl_Loaded(object sender, RoutedEventArgs e) { ///房号赛选 string RoomNum = tbTheCurrentHousingUnits.Text.Trim(); if (BtL != null) { BtL.Clear(); UC_WP.Children.Clear(); //清除指定控件 //List<UC_BtDynamic> uC_s = new List<UC_BtDynamic>(); //foreach (var item in UC_WP.Children) //{ // if (item is UC_BtDynamic) // continue; // var button = item as UC_BtDynamic; // string s = button.Name; // if (!button.Name.Contains("btAddButton")) // continue; // uC_s.Add(button); //} //foreach (UC_BtDynamic item in uC_s) //{ // UC_WP.Children.Remove(item); //} //动态生成那个加号 Button button = new Button() { Width = 100, Height = 100, Margin = new Thickness(10), // Background = "#39b54a"; IsEnabled = true, Background = System.Windows.Media.Brushes.Transparent, /// Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#39b54a")), Name = "btAddButton", }; button.Click += new RoutedEventHandler(BtAddButton_Click); button.MouseEnter += new MouseEventHandler(BtDongTaisheng); Uri uri = new Uri(@"D:\练手的项目\WPFMvvM\WPF_MvvMTest\WPF_MvvMTest\Image\加号.png", UriKind.Relative); ImageBrush ib = new ImageBrush(); ib.ImageSource = new BitmapImage(uri); button.Background = ib; UC_WP.Children.Add(button); } BtL = (from tbR in myModels.SYS_RoomStage join tbC in myModels.SYS_Class on tbR.ID_Class.ToString().Trim() equals tbC.Code_Class.ToString().Trim() where tbR.ID_Class == 1 orderby tbR.Describe select new RoomStage { ID_RoomStage = tbR.ID_RoomStage, ID_Class = tbR.ID_Class, Number_RoomStage = tbR.Number_RoomStage, MC_RoomStage = tbR.MC_RoomStage, State_RoomStage = tbR.State_RoomStage, Describe = tbR.Describe, JionSign = (int)tbR.JionSign, MC_Class = tbC.MC_Class, Jc_Class = tbC.Jc_Class, Code_Class = tbC.Code_Class, }).ToList(); //房号赛选 if (!string.IsNullOrEmpty(RoomNum)) { BtL = BtL.Where(l => l.Number_RoomStage == RoomNum).ToList(); } // BtL.Count(); #region 动态生成控件 for (int i = 0; i < BtL.Count(); i++) { //Button b = new Button() { Height = 100, // Width = 100, // //设置外边距 // Name = "btn" + 1, // Margin = new Thickness(10), // //按钮内容 // //Content = // // BtL[i].Number_RoomStage + // //@"\n" + BtL[i].MC_RoomStage, // Background = System.Windows.Media.Brushes.LightSeaGreen, UC_BtDynamic myBtDynamic = new UC_BtDynamic(BtL); myBtDynamic.TbRoom_num.Text = BtL[i].Number_RoomStage; myBtDynamic.TbRoom_class.Text = BtL[i].MC_RoomStage.Trim() + BtL[i].Number_RoomStage; myBtDynamic.Name = "btn" + BtL[i].ID_RoomStage; //换台刷新 myBtDynamic.pus += new Pus(Push); //获取控件内容id myBtDynamic.GetButtons += new GetButton(SetBtuuons); if (BtL[i].State_RoomStage.Trim() == "预定") { myBtDynamic.Background = System.Windows.Media.Brushes.Blue; } else if (BtL[i].State_RoomStage.Trim() == "已用") { myBtDynamic.Background = System.Windows.Media.Brushes.YellowGreen; } else if (BtL[i].State_RoomStage.Trim() == "停用") { myBtDynamic.Background = System.Windows.Media.Brushes.LightSlateGray; } // myBtDynamic.Click += new RoutedEventHandler(btnEvent_Click); UC_WP.Children.Add(myBtDynamic); } ; #endregion #region 动态生成控件 ////开始离开时间布局 //StackPanel stack = new StackPanel() //{ // Orientation = Orientation.Horizontal, // Background = System.Windows.Media.Brushes.Black, //}; ////开始时间 //TextBlock t = new TextBlock() //{ // FontSize = 7, // Text = DateTime.Now.ToString() +"vvvvvvvv"+ DateTime.Now.ToString(), //}; ////现在时间 ////单击事件 //b.Click += new RoutedEventHandler(btnEvent_Click); ////鼠标移入事件 //b.MouseMove += new MouseEventHandler(tbnMouseMove); ////鼠标移开事件 //b.MouseLeave += new MouseEventHandler(tbnMouseLeave); //UC_WP.Children.Add(b); //stack.Children.Add(t); #endregion //全部房数 tbAllContet.Text = BtL.Count().ToString(); //可用台 TbAvailable.Text = (BtL.Count() - BtL.Where(p => p.State_RoomStage.Trim() == "已用").Count()).ToString(); //已用台 TbHadToUse.Text = BtL.Where(p => p.State_RoomStage.Trim() == "已用").Count().ToString(); }