$this and $source message parameters

Jul 21, 2010 at 8:39 PM
Edited Jul 21, 2010 at 8:40 PM
I'm having difficulty understanding proper usage of $this and $source in message parameters. The main Caliburn documentation doesn't seem to be enlightening me. The following does not work for me like I would expect... [code] <TreeView ItemsSource="{Binding Items}" ItemContainerStyle="{StaticResource TreeViewItemStyle}" cal:Message.Attach="[Event SelectedItemChanged] = [Action SelectedItemChanged($source.SelectedItem)]"/> [code] with the following in my ViewModel... [code] void SelectedItemChanged(object selectedItem) /// accepting any object for test purposes [code] ...selectedItem is null. But when I use "$this" I do get what I want; selectedItem contains an object corresponding to the TreeView node that was selected. I can accept that. But what I find really confusing is that I get the same result (selectedItem containing what I want) when I use "$this.SelectedItem". If $this gets me my item, I wouldn't expect $this.SelectedItem to also get me my item. And if $source returns the FrameworkElement that raised the event, I would expect $source.SelectedItem to be the proper expression for referring to the item, not $this or $this.SelectedItem.
Jul 21, 2010 at 8:41 PM
by the way, what is the proper tag for formatting code on this message board?
Jul 21, 2010 at 8:51 PM

You just encountered one of the "Micro" aspects of Caliburn.Micro. 'Dotting" only works with $this because it is hooked up by databinding at parse time. $source and $eventArgs only pass the value at invocation time. Hmm...perhaps $source is redundant and should be removed? Thus $this will be the only way? Thoughts?

Jul 21, 2010 at 8:53 PM
Edited Jul 21, 2010 at 8:53 PM

Also, I'm not sure what the tag for embedding code on the forums is. Sometimes there's a toolbar for the text editor that lets you embed code.

Jul 21, 2010 at 9:23 PM
Isn't $source how you provide access to the FrameworkElement that raised the event? I think the most important items are FrameworkElement, DataContext and EventArgs. Everything else should be obtainable either directly or indirectly through one of those.