tag:blogger.com,1999:blog-7577421612120825312.post3923894827230227612..comments2023-10-03T10:41:13.944+01:00Comments on Functional Fun: WPF MVVM Commands - new reduced-boilerplate recipeAnonymoushttp://www.blogger.com/profile/01345100698738870730noreply@blogger.comBlogger10125tag:blogger.com,1999:blog-7577421612120825312.post-45848591846615986392011-06-23T17:15:45.836+01:002011-06-23T17:15:45.836+01:00This use of CanExecute in this implementation did ...This use of CanExecute in this implementation did not work for me, in cases where the CanExecute trigger was a changeable property within the same WPF window.<br /><br />I was able to get it working by replacing:<br />public event EventHandler CanExecuteChanged;<br />protected void InvokeCanExecuteChanged(){ EventHandler handler = CanExecuteChanged; if (handler != null) handler(this, EventArgs.Empty);}<br /><br />With:<br />public event EventHandler CanExecuteChanged{ add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; }}Joshua Collisonnoreply@blogger.comtag:blogger.com,1999:blog-7577421612120825312.post-86463375319956604392011-05-16T15:49:27.244+01:002011-05-16T15:49:27.244+01:00@Scott That is an awesome refactoring. I've pu...@Scott That is an awesome refactoring. I've put the Commands property on my base ViewModel, and now the amount of code in the concrete ViewModels is even smaller, and everything is refactoring-friendly.David Keavenyhttp://www.blogger.com/profile/07944978161341604262noreply@blogger.comtag:blogger.com,1999:blog-7577421612120825312.post-79801145904096997752011-02-03T02:53:14.567+00:002011-02-03T02:53:14.567+00:00@Scott That is an awesome refactoring. I've pu...@Scott That is an awesome refactoring. I've put the Commands property on my base ViewModel, and now the amount of code in the concrete ViewModels is even smaller, and everything is refactoring-friendly.David Keavenyhttps://www.blogger.com/profile/07944978161341604262noreply@blogger.comtag:blogger.com,1999:blog-7577421612120825312.post-23669395423129760392010-10-28T16:30:51.911+01:002010-10-28T16:30:51.911+01:00my opinion: over-engineered. adding obscurity and...my opinion: over-engineered. adding obscurity and a slight bit of complexity without gaining enough in return.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7577421612120825312.post-16269669344130896102010-08-21T19:50:57.268+01:002010-08-21T19:50:57.268+01:00@Anonymous, which day did you run it on? Take a lo...@Anonymous, which day did you run it on? Take a look at the expression that determines whether the button is enabled (in IsFeelingDemonstrative) ...Anonymoushttps://www.blogger.com/profile/01345100698738870730noreply@blogger.comtag:blogger.com,1999:blog-7577421612120825312.post-65163324596884968892010-08-21T14:14:08.944+01:002010-08-21T14:14:08.944+01:00I'm new to WPF/MVVM
When I run your example (t...I'm new to WPF/MVVM<br />When I run your example (thanks for sharing btw) the button is disabledAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-7577421612120825312.post-15115527336051435062010-06-27T00:22:14.601+01:002010-06-27T00:22:14.601+01:00I haven't decided if I like this yet or not, b...I haven't decided if I like this yet or not, but another option is C# 4's dynamic objects. Even less boilerplate with something like:<br /><br /> public class MyViewModel<br /> {<br /> private dynamic commands = new ExpandoObject();<br /> public dynamic Commands { get { return commands; } }<br /><br /> public MyViewModel()<br /> {<br /> Commands.TestCommand = new DelegateCommand(x => MessageBox.Show("Test", "TaDa!"));<br /> }<br /> }Scott Bussingerhttp://bussinger.org/blognoreply@blogger.comtag:blogger.com,1999:blog-7577421612120825312.post-4940790224885185612010-06-25T12:05:54.535+01:002010-06-25T12:05:54.535+01:00Oh, that's correct, I never noticed the Comman...Oh, that's correct, I never noticed the CommandHolder instance inside the ViewModel. Grr, next time will read thoroughly :-)Román Fresneda Quirogahttps://www.blogger.com/profile/15765341519444055469noreply@blogger.comtag:blogger.com,1999:blog-7577421612120825312.post-91094313995559830312010-06-25T11:29:55.576+01:002010-06-25T11:29:55.576+01:00Roman,
Good to hear from you!
Since the lifeti...Roman,<br /> Good to hear from you! <br /><br />Since the lifetime of the CommandHolder is bound to the ViewModel (through its base class), I don't see that there will be any lifetime issues caused by the delegates. If the CommandHolder instance was static, that would be a different matter.Anonymoushttps://www.blogger.com/profile/01345100698738870730noreply@blogger.comtag:blogger.com,1999:blog-7577421612120825312.post-33896584451461975272010-06-25T11:26:10.904+01:002010-06-25T11:26:10.904+01:00Hmmm, interesting, although I'd like you to me...Hmmm, interesting, although I'd like you to mention how lifetime issues with those viewmodels and the delegates held by the CommandHolder are avoided, or am I losing sight?<br /><br />Cheers, your _son_ :-)Román Fresneda Quirogahttps://www.blogger.com/profile/15765341519444055469noreply@blogger.com