I am writing a WPF app, and need to be able to bind a method call with arguments to a button's Click event (or equivalent).
Until now, just for testing I was programmatically creating the buttons and setting them as children of the necessary StackPanel.
foreach (Page p in pages)
{
Button newTestBtn = new Button();
newTestBtn.Content = p.Title;
newTestBtn.Name = p.Name.Replace(" ", string.Empty);
newTestBtn.FontSize = 14;
newTestBtn.Margin = new Thickness(2, 0, 2, 2);
newTestBtn.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF414141"));
newTestBtn.Click += (s, e) => { SwitchPage(p); };
hp.CreateNewTestEntry(newTestBtn);
}
After deciding this was far from the best way of doing this, I set up a more permanent solution by having an ItemsControl bound to an ObservableCollection of a serialized class.
I have hit a new problem though, applying an ICommand or RoutedEventArg to a Click or Command on a button simply doesn't seem to work.
foreach (Page p in pages)
{
TempCl tcl = new TempCl();
tcl.ToolName = p.Title;
tcl.TriggerEvent = SwitchPage(p);
tempHolder.Add(tcl);
}
Google & StackOverflow searching hasn't yielded any easy ways of doing this and I'm a bit stuck.
<Button Margin="0, 0, 0, 2" Content="{Binding ToolName}" Command="{Binding TriggerEvent}" FontSize="14" Background="#FF414141"/>
Does anyone know the correct way of doing this without adding a lengthy class to do this? There must be an elegant solution.