I'm generating buttons dynamically depending on the contents of a file.
My code only executes the RelayCommand once if I write it in this way:
var button = new Button();
button.Command = new RelayCommand(() => Messenger.Default.Send(new UseThisValue(value));
// button gets added to a treeview
but if I change it so that the lambda is set to the Tag propery it works every time
var button = new Button();
button.Tag = new Action(() => Messenger.Default.Send(new UseThisValue(value));
button.Command = new RelayCommand(button.Tag as Action);
// button gets added to a treeview
What is going on here? Is the assignmend keeping some reference count alive? I'm fairly new to C# so I might be overlooking something obvious to the more experienced developer.