2013-09-25 94 views
2

我有一個按鈕,每次單擊它時,都會將此StackPanel添加到列表框中。它是一個按鈕。我試圖找出如何將代碼添加到它添加的這個按鈕。理想情況下,我希望按鈕是一個刪除按鈕,因此它會刪除列表中的該元素(本身)。我只是想弄清楚如何爲我動態創建的按鈕添加功能。希望有道理WPF/C# - 向列表框中動態創建的按鈕添加功能

感謝您的任何幫助!

private void Button_Click_1(object sender, RoutedEventArgs e) 
    { 
     StackPanel stackPanel = new StackPanel(); 
     stackPanel.Orientation = System.Windows.Controls.Orientation.Horizontal; 

     CheckBox checkBox = new CheckBox(); 
     checkBox.IsChecked = true; 

     TextBox textBox = new TextBox(); 
     textBox.Width = 100; 
     textBox.Text = textBox1.Text; 

     Button button = new Button(); //HOW DO I ADD CODE TO THIS BUTTON? 

     stackPanel.Children.Add(checkBox); 
     stackPanel.Children.Add(textBox); 
     stackPanel.Children.Add(button); //HOW DO I ADD CODE TO THIS BUTTON? 

     listBox1.Items.Add(stackPanel); 
    } 

回答

1

試試這個。

添加StackPanel中有文本塊和按鈕

private void OnSaveClick(object sender, RoutedEventArgs e) 
     { 
      StackPanel stp = new StackPanel(); 
      stp.Orientation = Orientation.Horizontal; 
      stp.Children.Add(new TextBlock() 
      { 
       Text = string.Format("Item {0}", lstitems.Items.Count), 
        HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch 
      }); 

      Button btn = new Button(); 
      btn.Content = string.Format("Delete Item {0}", lstitems.Items.Count); 
      btn.Height = 25; 
      btn.Width = 100; 
      btn.HorizontalAlignment = System.Windows.HorizontalAlignment.Right; 
      btn.Click += btnDeleteClick; 


      stp.Children.Add(btn); 

      lstitems.Items.Add(stp); 
     } 

刪除按鈕單擊處理程序

void btnDeleteClick(object sender, RoutedEventArgs e) 
     { 
      Button btn = (Button)sender; 
      if (btn != null) 
      { 
       var st = FindParent<StackPanel> (btn); //stackpanel as we have added item as stackpanel. 
       if (st != null) 
        lstitems.Items.Remove(st); 
      } 
     } 

找到類型在Visual樹到對象。

public T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject 
    { 
     var parent = VisualTreeHelper.GetParent(dependencyObject); 

     if (parent == null) return null; 

     var parentT = parent as T; 
     return parentT ?? FindParent<T>(parent); 
    } 
+0

我實際上結束了使用這個解決方案,因爲它幫助我刪除了它在哪個行中是我正在尋找的行。謝謝 – user1189352

2

您可以點擊處理程序編程添加到按鈕這樣的:

Button button = new Button(); //HOW DO I ADD CODE TO THIS BUTTON? 
    button.Click += btn_Click; 

    stackPanel.Children.Add(checkBox); 
    stackPanel.Children.Add(textBox); 
    stackPanel.Children.Add(button); //HOW DO I ADD CODE TO THIS BUTTON? 

,然後你需要的單擊事件處理程序

void btn_Click(object sender, System.Windows.RoutedEventArgs e) 
    { 
     // your code to execute when the button is clicked. 
     stackPanel.Items.Remove(button); 
    } 
+0

如果它不是'button.Click + =新RoutedEventHandler(btn_Click);' – user2378527

+0

的代碼是錯誤的:'stackPanel'加到'listBox1',不'button'。 – meilke

+0

@meilke啊,是的好點,固定 – Dutts

1

這是最簡單的設置。理想情況下,你想要更多的錯誤處理等。

Button button = new Button(); 
button.Click += (s, args) => { listBox1.Items.Remove(stackPanel); }; 
+0

還沒有嘗試過,但我喜歡這個解決方案最好,如果它的工作。不錯,簡單 – user1189352