2

我编写了动态创建按钮的代码,但是如何为每个按钮分配不同的功能?

for (int i = 0; i < Buttons.Count; i++)
{
            Button newBtn = new Button();
            newBtn.Content = Buttons[i];
            newBtn.Name = "Button" + i.ToString();
            newBtn.Height = 23;
            stackPanel1.Children.Add(newBtn);
            newBtn.Click += new RoutedEventHandler(newBtn_Click);
}

private void newBtn_Click(object sender, RoutedEventArgs e)
{
        MessageBox.Show("Hello");
}

现在每个按钮都显示“Hello”,但我希望它是“Hello1”、“Hello2”……等等。

4

5 回答 5

6

如果您可以使用 DelegateCommands 或 RelayCommand 属性和 DisplayName 属性创建对象集合 - 您只需要绑定到此集合的 ItemsControl 和用于将按钮绑定到命令和文本的 DataTemplate。

编辑:就在我脑海中

 public class MyCommandWrapper
 {
    public ICommand Command {get;set;}
    public string DisplayName {get;set;}
 }

在您的视图模型中

 public ObservableCollection<MyCommandWrapper> MyCommands {get;set;}

 MyCommands.Add(new MyCommandWrapper(){Command = MyTestCommand1, DisplayName = "Test 1"};
 ...

在你的xml中

  <ItemsControl ItemsSource="{Binding MyCommands}">
   <ItemsControl.Resources>
     <DataTemplate DataType="{x:Type local:MyCommandWrapper}">
       <Button Content="{Binding DisplayName}" Command="{Binding Command}"/>
     </DataTemplate>
   </ItemsControl.Resources>
  </ItemsControl>

编辑 2:如果您需要一个新的动态按钮 - 只需在您的收藏中添加一个新的包装器

于 2013-10-22T12:07:46.730 回答
2
  for (int i = 0; i < Buttons.Count; i++)
    {
                Button newBtn = new Button();
                newBtn.Content = Buttons[i];
                newBtn.Height = 23;
                newBtn.Tag=i;
                stackPanel1.Children.Add(newBtn);
                newBtn.Click += new RoutedEventHandler(newBtn_Click);
    }

private void newBtn_Click(object sender, RoutedEventArgs e)
{
       Button btn=sender as Button;
       int i=(int)btn.Tag;

       switch(i)
       {
         case 0:  /*do something*/ break;
         case 1:  /*do something else*/ break;
         default: /*do something by default*/ break;
       }
}
于 2013-10-22T12:11:49.603 回答
1
private void newBtn_Click(object sender, RoutedEventArgs e)
{
        var button = sender as Button;
        var buttonNumber = button.Name.Remove(0, 6);

        MessageBox.Show("Hello" + buttonNumber);
}
于 2013-10-22T12:04:43.347 回答
1

检查函数的sender参数newBtn_Click。它应该包含被点击的按钮的实例。您可以将其转换为按钮并检查名称:

private void newBtn_Click(object sender, RoutedEventArgs e)
{
    var btn = sender as Button;
    if(btn != null)
    {
        MessageBox.Show(btn.Name);
    }
}

如果您不想检查 Name 属性,也可以使用 Tag 属性 ( http://msdn.microsoft.com/library/system.windows.frameworkelement.tag.aspx ),您可以为其分配任意对象并稍后检查:

Button newBtn = new Button();
newBtn.Tag = i;

稍后在点击处理程序中:

private void newBtn_Click(object sender, RoutedEventArgs e)
{
    var btn = sender as Button;
    if(btn != null)
    {
        if(btn.Tag is int)
            MessageBox.Show(String.Format("Hello{0}", btn.Tag));
    }
}

我更喜欢使用 Tag 的解决方案,因为它比从字符串中提取内容更安全。

于 2013-10-22T12:06:01.070 回答
1
newBtn.Click += new RoutedEventHandler((s,e) => MessageBox.Show("hallo"+((Button)s).Name);
于 2013-10-22T12:09:58.077 回答