2012-03-23 161 views
1

所以這裏的代碼動態地將按鈕添加到我的wpf windows應用程序中。我不能想到我可以調用實際上運行服務器的按鈕的方式,因爲它們是隨機添加的。隨機分配的按鈕

namespace DynamicButtons 
{ 
    public partial class Window1 
    { 
     public Window1() 
     { 
      this.InitializeComponent(); 

      populateButtons(); 
     } 

     public void populateButtons() 
     { 
      int xPos; 
      int yPos; 

      Random ranNum = new Random(); 

      for (int i = 0; i < 4; i++) 
      { 
       Button foo = new Button(); 
       Style buttonStyle = Window.Resources["CurvedButton"] as Style; 

       int sizeValue = ranNum.Next(100); 

       foo.Width = sizeValue; 
       foo.Height = sizeValue; 

       xPos = ranNum.Next(200); 
       yPos = ranNum.Next(300); 

       foo.HorizontalAlignment = HorizontalAlignment.Left; 
       foo.VerticalAlignment = VerticalAlignment.Top; 
       foo.Margin = new Thickness(xPos, yPos, 0, 0); 

       foo.Style = buttonStyle; 
       foo.Name = "button" + i; 

       foo.Click += new RoutedEventHandler(buttonClick); 

       LayoutRoot.Children.Add(foo); 
      } 
     } 

     private void buttonClick(object sender, EventArgs e) 
     { 
      Button clicked = (Button) sender; 
      // something like certainWindowsButton((i)<-this has to be based on above code) = clicked.Name(); 
      MessageBox.Show("Button's name is: " + clicked.Name); 
     } 
    } 
} 

應用程序運行的按鈕被隨機分配每一次,我想是在我可以從他們後面的代碼進行交互的方式。

所以

foo.Name = "button" + i; 

時指或點擊任何一個按鈕,一個號碼分配給它,但我怎麼可以在代碼按鈕互動?

private void buttonClick(object sender, EventArgs e) 
    { 
     Button clicked = (Button) sender; 
     // something like certainWindowsButton((i)<-this has to be based on above code) = clicked.Name(); 
     MessageBox.Show("Button's name is: " + clicked.Name); 
    } 
} 

我希望這是有道理的。

爲了給你的感覺什麼在設計層面發生:

enter image description here

每個灰色方塊是按鈕,每個按鈕被隨機分配其指定的名稱和號碼,每次應用程序運行這些按鈕將被重新分配一個不同的號碼。

我需要一種方式,當一個數字被分配給它時,我可以點擊該按鈕時觸發一個事件。

所以隨機按鈕已被分配的號碼1,取這個數字1,然後將其分配給

private void buttonClick(object sender, EventArgs e) 

if 
{ 
    button1_Click = clicked.Name(strip away "button" and make sure the (variableNumber leftover matches button(1)_Click); 
} 
else 
    button2 

等等等等

然後觸發我的按鈕事件:

 private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      MessageBox.Show("yay each randomly assigned button now correlates with a real button event"); 
     } 
    } 
} 
+0

'clicked.Name'不會給你你想要的名字嗎?它看起來應該這樣做...發件人參數的一個事件處理程序的整個觀點是,它包含觸發事件的對象,即單擊的按鈕... – Chris 2012-03-23 17:48:43

+0

是的,但是如何將button1綁定到某個東西在代碼後面?如果我想要「button1」打開內容,例如我怎樣才能在按鈕1被按下後的代碼中調用它? – 2012-03-23 17:54:31

+0

我不希望所有的按鈕做同樣的事情,所有這一切都是改變按鈕名稱時,我按下按鈕,如果我想要一個按鈕來顯示一個消息框,但另一個按鈕來打開另一個窗口如何區分二? – 2012-03-23 17:59:48

回答

1

這更多是你在找什麼?這需要每個buttonNClick處理程序併爲其創建一個按鈕。這樣你甚至不需要「我」。

partial class Window1 { 
    void button3Click(object sender, RoutedEventArgs e) { 
     //Whatever you want here 
    } 
    void button2Click(object sender, RoutedEventArgs e) { 
     //Whatever you want here 
    } 
    void button1Click(object sender, RoutedEventArgs e) { 
     //Whatever you want here 
    } 

    public Window1() { 
     this.InitializeComponent(); 
     populateButtons(); 
    } 

    public void populateButtons() { 
     int xPos; 
     int yPos; 

     Random ranNum = new Random(); 
     foreach (var routedEventHandler in new RoutedEventHandler[] { button1Click, button2Click, button3Click }) { 
      Button foo = new Button(); 

      int sizeValue = ranNum.Next(100); 

      foo.Width = sizeValue; 
      foo.Height = sizeValue; 

      xPos = ranNum.Next(200); 
      yPos = ranNum.Next(300); 

      foo.HorizontalAlignment = HorizontalAlignment.Left; 
      foo.VerticalAlignment = VerticalAlignment.Top; 
      foo.Margin = new Thickness(xPos, yPos, 0, 0); 

      foo.Click += routedEventHandler; 

      LayoutRoot.Children.Add(foo); 
     } 
    } 
} 
+0

對不起,你可以在我的代碼中顯示,而不是「dowhatyouwant」也許fire' button1_Click'? – 2012-03-23 17:47:11

+0

@Kirsty不確定你的意思。您可以使用反射API來執行諸如(僞代碼)Reflect.GetMethod(this.Type,「button」+ i2 +「_click」).execute();',但我認爲這不是最好的選擇。 – lobsterism 2012-03-23 17:58:41

+0

'foo.Name =「button」+ i;'它看起來像它幾乎正確,但這條線有問題。 – 2012-03-23 18:18:41

0

由於龍蝦說,或者你可以有一個命令列表,你隨機綁定到按鈕,而不是任何這些。 http://www.switchonthecode.com/tutorials/wpf-tutorial-command-bindings-and-custom-commands

這裏是一個詳細的例子:

public class CoolCommand : ICommand 
{ 
    #region ICommand Members 

    public bool CanExecute(object parameter) 
    { 
     return true; 
    } 

    public event EventHandler CanExecuteChanged; 

    public void Execute(object parameter) 
    { 
     //do what you want :) 
    } 

    #endregion 
} 

var allCommands = new List<ICommand>(); 
allCommands.Add(new CoolCommand); 
allCommands.Add(new OtherCoolCommand); 
allCommands.Add(new ThirdCoolCommand); 

private void assignButton(button) 
{ 
    var idx = new Random().nextInt(allCommands.Count-1); 
    var command = allComands[idx]; 
    button.Command = command; 
    allCommands.remove(command); 
} 
1

如果你只是想選擇基於按鈕的動作名稱下面的代碼可能會有所幫助。 doAction方法調用顯然會隨着你的情況而改變,但希望代碼能給你一個足夠好的一般想法。

private void buttonClick(object sender, EventArgs e) 
    { 
     Button clicked = (Button) sender; 
     ChooseAction(clicked.Name); 
    } 

    private void ChooseAction(string buttonName) 
    { 
     switch(buttonName) 
     { 
      case "button1": doAction1(); break; 
      case "button2": doAction2(); break; 
      case "button3": doAction3(); break; 
      case "button4": doAction4(); break; 
      case "button5": doAction5(); break; 
      default: doDefaultAction(); break; 
     } 
    } 

    private void doAction1() 
    { 
     Console.WriteLine("action 1"); 
    } 

    private void doAction2() 
    { 
     Console.WriteLine("action 2"); 
    } 
    private void doAction3() 
    { 
     Console.WriteLine("action 3"); 
    } 
    private void doAction4() 
    { 
     Console.WriteLine("action 4"); 
    } 
    private void doAction5() 
    { 
     Console.WriteLine("action 5"); 
    } 

    private void doDefaultAction() 
    { 
     Console.WriteLine("button name not recognised, performing default action"); 
    }