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.
-
1By 'create dynamically buttons' do you mean creating the buttons off of some collection? or creating a button every time some condition is satisfied? Either way is possible, I just want to understand what you are looking for.Ivan Vargas– Ivan Vargas2017-02-03 17:47:33 +00:00Commented Feb 3, 2017 at 17:47
-
1Yes it's possible and easy to do as taquion and Arman responded. However, that doesn't mean that it's a good idea! It's really best to keep UI controls in the XAMLGreatJobBob– GreatJobBob2017-02-03 18:15:49 +00:00Commented Feb 3, 2017 at 18:15
-
I would like to create several buttons, but depends on some condition in one scenario it will be 3 buttons in another scenario it will be 5 buttons and so on. So if it's really best to keep UI in XAML I could create all the buttons in XAML and then only change their visibility depending on condition value, am I right ?KinectUser– KinectUser2017-02-03 19:07:56 +00:00Commented Feb 3, 2017 at 19:07
-
You probably want an ObservableCollection bound to a ListBox with the ListBoxItemTemplate containing a button.. The click event of the button looks at the sender and then gets the DataContext. The code can they do different things depending on the DataContext. You can also do it in CodeBehind but its generally not recommended.patrick– patrick2017-02-03 19:19:33 +00:00Commented Feb 3, 2017 at 19:19
-
Patrick can you show me some kind of example ? link ? or a piece of code ? because I would like understand better what did you meanKinectUser– KinectUser2017-02-03 20:18:46 +00:00Commented Feb 3, 2017 at 20:18
Add a comment
|
3 Answers
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();
}
Comments
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.
1 Comment
KinectUser
You don't understand me, I want to create for example 2 buttons and separate click event for each of this button
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
1 Comment
KinectUser
thank you, but I must admit that I don't understand that well. I think I need to read something more about that