2012-03-23 20 views
1

です。このコードは、ここで動的にボタンを私のwpfウィンドウアプリケーションに追加します。ランダムに追加されているため、実際にrunatサーバーと呼ばれるボタンを呼び出すことはできません。ランダムに割り当てられたボタン

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

あなたはあなたが望む名前を与えていませんか?イベントハンドラへの送信者パラメータの全体的なポイントは、イベントをトリガしたオブジェクト、つまりクリックされたボタンが含まれているということです。 – Chris

+0

ええ、どうにかボタン1を何かにバインドしますかコードの背後にある?たとえば "button1"にコンテンツを開くには、button1を押したときにコードの中でどのように呼び出すことができますか? –

+0

私はボタンを押したときに表示されるボタン名を変更します。メッセージボックスを表示するボタンが1つだけで、別のウィンドウを開くボタンが必要な場合は、どのボタンを押しても同じように表示されます。二? –

答えて

1

これはあなたが探しているものですか?これは、各buttonNClickハンドラを取り、そのボタンを作成します。この方法では、「i」は必要ありません。

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"の代わりに "button1_Click"を発射することができますか? –

+0

@Kirstyあなたは何を意味するのかよく分かりません。 Reflect.GetMethod(this.Type、 "button" + i2 + "_click")。Execute(); 'のような(擬似コード)を行うためにリフレクションAPIを使用することができましたが、これは最良の選択肢ではないと思います。 – lobsterism

+0

'foo.Name =" button "+ i;'それはほぼ正しいようですが、この行に問題があります。 –

0

ロブスターが言ったように、またはコマンドのリストを持つことができ、ボタンの代わりにランダムにボタンをバインドすることができます。あなただけのボタン名に以下のコードをベースとアクションが役立つことを選択したい場合は

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

:ここhttp://www.switchonthecode.com/tutorials/wpf-tutorial-command-bindings-and-custom-commands

は冗長なサンプルです。もちろん、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"); 
    } 
関連する問題