wpf - Caliburn.Micro: Binding Button in a Screen to a command in ScreenConductor -
i'm following this tutorial on screen
, screenconductors
in caliburn.micro
framework.
i'm using wpf, not silverlight, , have made corresponding changes in app.xaml (using mergeddictionary bootstrapper).
the original simple navigation example has shell 2 buttons, , content area 2 possible screens displayed, conducted shellviewmodel
.
then tried move each button counterpart view, pageone have button take pagetwo, , vice-versa. did because don't want home shell continuously "showing entrails" across application.
the fact is, if move button screen
view, no longer binds command, in shellviewmodel
, not in screen
viewmodel itself. know these bindings happen convention, don't know if convention covers case, or i'd need configure.
the symptom facing is: when run app, pageoneview shows, "go page two" button in it, when click button nothing happens.
the question ask is: "how proper way "bubble" button in screenview.xaml action in screenconductorviewmodel.cs?
my current code below:
pageoneview.xaml
<usercontrol x:class="screenconductor.pageoneview" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:ignorable="d" d:designheight="300" d:designwidth="300"> <grid background="lightgreen"> <button x:name="showpagetwo" content="show page two" horizontalalignment="center" verticalalignment="top" /> </grid> </usercontrol>
pageoneviewmodel
using caliburn.micro; namespace screenconductor { public class pageoneviewmodel : screen { protected override void onactivate() { base.onactivate(); } } }
shellview.xaml
<window x:class="screenconductor.shellview" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525"> <contentcontrol x:name="activeitem" /> </window>
shellviewmodel.cs
using caliburn.micro;
namespace screenconductor { public class shellviewmodel : conductor<object> { public shellviewmodel() { showpageone(); } public void showpageone() { activateitem(new pageoneviewmodel()); } public void showpagetwo() { activateitem(new pagetwoviewmodel()); } } }
as long bind command using explicit action binding syntax bubbling should work fine. because bind name cm examines controls on view bound current vm. if there no matching named control binding not created.
you can use this:
<somecontrol cal:message.attach="somemethodonparentvm" />
this attempt bubble message control hierarchy until suitable handler found. note throw exception if no handler found. recall there being option turn off on binding might want check docs (i'm contributing mobile @ moment :-)
edit:
ok if want more info behind scenes, best place check source. author rob eisenberg mentions source quite small (something 2700 lines of code think) it's easy inspect , easy hold in head
it's worth reading through documentation on codeplex site (there's fair few pages explain in enough detail piece rest).
not sure how know message
class contains attach
attached property kicks off action binding when standard control name
conventions aren't used (i assume know attached properties in wpf/sl). can see standard named conventions in viewmodelbinder
class
the message
class looks like:
(yes it's silverlight source base other versions , it's few compiler directives , additional classes appear in other version such wpf/winrt)
if @ source can see when attached property set, parser
class kicks off parse string. parser parses several different formats can attach different events , methods, , pass properties e.g.
<button cal:message.attach="[event click] = [action somebuttonwasclicked()]" />
or
<button cal:message.attach="[event mouseenter] = [action mouseenteredabutton($eventargs)" />
above can see $eventargs
special value used. there several out-of-box special values, , can write own (check out question using messagebinder.specialvalues in windows 8 app not working? user using specialvalues
pass horizontal mouse position controls use in synthesizer app)
you can pass cm default property of other controls e.g.
<textbox x:name="textbox1" /> <button cal:message.attach="mouseclicked(textbox1)" />
where text
value of textbox1
passed mouseclicked
method on vm. specified in default convention bindings textbox
(look @ conventionmanager.addelementconvention
, docs on that)
the bubbling works inspecting visual tree , attempting bind each level (happens in setmethodbinding
in actionmessage
class)
it's pretty simple effective (it uses visualtreehelper
walk visual tree until suitable handler found)
not sure else might need info on :p
Comments
Post a Comment