KinectUser
KinectUser

Reputation: 31

C# - WPF - create buttons and click event programatically

I have a question. Is it possible to create dynamically buttons and click events to it ? For example I want to create 4 buttons with 4 different click events. It is not necessary to make it with MVVM pattern. At the begining I would like just to know is it possible and how can I achieve this.

Upvotes: 2

Views: 16125

Answers (3)

Ivan Vargas
Ivan Vargas

Reputation: 703

One possible way is binding the ItemsSource property to a collection in your view model, i.e.

   <ItemsControl ItemsSource="{Binding Commands}">
      <ItemsControl.ItemTemplate>
         <DataTemplate>
             <Button Command="{Binding Command}" Content="{Binding DisplayName}" />
         <DataTemplate>
      <ItemsControl.ItemTemplate>
   </ItemsControl>

This is of course using MVVM. The View Model would have some kind of collection of CommandViewModel, something like

   public class ControlViewModel
   {
      public Collection<CommandViewModel> Commands { get; }

      public ControlViewModel()
      {
           // Here have logic to determine which commands are added to the collection
         var command1 = new RelayCommand(p => ActionForButton1());
         var command2 = new RelayCommand(p => ActionForButton2());

         Commands = new Collection<CommandViewModel>();
         Commands.Add(new CommandViewModel(command1, "Button 1"));
         Commands.Add(new CommandViewModel(command2, "Button 2"));

      }

      private void ActionForButton1()
      {
         // ....
      }

      private void ActionForButton2()
      {
         // ....
      }
   }

Some of the classes (CommandViewModel and RelayCommand) are taken from Josh Smith's article. I just typed code in here, you might want to double check there are no syntax errors.

I hope it helps

Upvotes: 0

taquion
taquion

Reputation: 2767

Yeap.

 public MainWindow()
    {
        InitializeComponent();

        var btn1 = new Button{Content = "btn1"};

        //add event handler 1
        btn1.Click += ClickHandler1;

        //removes event handler 1
        btn1.Click -= ClickHandler1;

        //add event handler 2
        btn1.Click += ClickHandler2;

        Panel.Children.Add(btn1);

    }

    private void ClickHandler1(object sender, RoutedEventArgs e)
    {
        //do something
    }

    private void ClickHandler2(object sender, RoutedEventArgs e)
    {
        //do something
    }

    private void ClickHandler3(object sender, RoutedEventArgs e)
    {
        //do something

    }

You can have several event handlers and add and remove them as needed.

Upvotes: 1

Arman Nagaepetian
Arman Nagaepetian

Reputation: 19

Yes it is possible :

 public MainWindow()
    {
        InitializeComponent();

        var button = new Button() {Content = "myButton"}; // Creating button
        button.Click += Button_Click; //Hooking up to event
         myGrid.Children.Add(button); //Adding to grid or other parent

    }

    private void Button_Click(object sender, RoutedEventArgs e) //Event which will be triggerd on click of ya button
    {
        throw new NotImplementedException();
    }

Upvotes: 1

Related Questions