Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / June 2007

Tip: Looking for answers? Try searching our database.

ICommand woes...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lloyd Dupont - 27 Jun 2007 05:58 GMT
I tried to create a SetColor command and a ColorPicker which uses it.

There is one effect I cannot reproduce, it is tracking the CommandSource and
wether it's valid.

By that I mean if I create a MenuItem like that:

<MenuItem Header="Blue" Command="{x:Static local:MyCommands.SetColor}"
CommandParameter="{x:Static Colors.Blue}" />

It enables/disables depending on wether the currently focused element
respond to the SetColor command.

But my ColorPicker never get any CanExecute or whatever event call,
therefore I cannot update it appropriately (i.e. enable/disable) when the
focused element change.

I implemented ICommandSource, used a RoutedEvent, I never get any relevant
info....

Any idea what I could have missed?!?
Lloyd Dupont - 27 Jun 2007 06:34 GMT
found it!

it's quite tricky, I remember read about it in a blog 6 month ago but it's
nowhere in the documentation!!!

RoutedCommand.CanExecuteChanged strore the event handlers as WeakReference.
Therefore you have to hold a reference to the handlers your self if you want
your event handler to be called....
Radek Cerny - 28 Jun 2007 00:13 GMT
Well done!

I am playing in the same area and am sure to have come across this soon.

Thanks.

> found it!
>
[quoted text clipped - 6 lines]
> want
> your event handler to be called....
Lloyd Dupont - 28 Jun 2007 02:04 GMT
Thanks!! :-D

Additional tip:

I was trying to get a value from the command source:
I.e. I have a color picker, I want it to be initialized to the current color
of the SetColorCommand target.
There is no way to do that!

But... little trick....
ICommand.CanExecute(object obj) take any object as argument.

I used something like:
public class CommandCanExecuteArg
{
   object val;
   public CommandCanExecuteArg(object val)
   {
       this.val = val;
   }
   public object Parameter {  get { return val; } }
   public object ReturnValue { get; set; }
}

and I pass an instance of it to CanExecute(), this way the
SetColorCommandTarget can return its current color in ReturnValue

Signature

Regards,
Lloyd Dupont
NovaMind Software
Mind Mapping at its best
www.nova-mind.com

> Well done!
>
[quoted text clipped - 13 lines]
>> want
>> your event handler to be called....

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.