wpf動態增加刪除控件


我在xaml中定義了一個名字為morepictureWrapPan為WrapPanel,然后將控件添加在此WrapPanel中。由於要實現控件的刪除功能,所以增加的textbox和button的名字都是有規律的

 int textboxcount = 0;
        private void DynameAddBTN()
        {
            TextBox tb = new TextBox();
            string countstr = "d" + textboxcount;
            tb.Name = "uploa" + countstr + "TB";
            tb.IsEnabled = false;
            Button btn = new Button();
            btn.Name = "uploa" + countstr + "BTN";
            btn.Content = "Upload";
            btn.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFD2E0EF"));
            //btn.Style = Resources["NormalBTNStyle"] as Style;
            //上傳圖片到服務器按鈕的點擊事件
            btn.Click += UploadPictureDynamic;
            Button btn2 = new Button();
            btn2.Name = countstr;
            btn2.Content = "x";
            btn2.Background = null;
            btn2.Margin = new Thickness(0, 0, 9, 0);
            btn2.Click += closepic_Click;
            morepictureWrapPan.Children.Add(tb);
            morepictureWrapPan.RegisterName("uploa" + countstr + "TB",tb);
            morepictureWrapPan.Children.Add(btn);
            morepictureWrapPan.RegisterName("uploa" + countstr + "BTN", btn);
            morepictureWrapPan.Children.Add(btn2);
   
            textboxcount++;
        }

  

 刪除動態控件的功能實現

 //動態刪除控件
        private void closepic_Click(object sender, RoutedEventArgs e)
        {
Button btn = sender as Button; Button closebtn = FindName("uploa" + btn.Name + "BTN") as Button; TextBox closetb = FindName("uploa" + btn.Name + "TB") as TextBox; //移除子節點的textbox、button morepictureWrapPan.Children.Remove(closebtn); morepictureWrapPan.Children.Remove(closetb); morepictureWrapPan.Children.Remove(btn); }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM