13

This is my XAML View (some code omitted for readability):

<Window ... xmlns:c="http://www.caliburnproject.org">
  <Button Content="Close without saving" c:Message.Attach="Close(false)" />
  <Button Content="Save and Close" c:Message.Attach="Close(true)" />
</Window>
And here's the code in the ViewModel:
public void Close(bool save) 
{
  if (save) 
  { 
    // save the data 
  }
  TryClose();
}
This doesn't work - of course - because the action parameters "true" and "false" aren't objects or object properties in the XAML. How can I make this work, and send a boolean as an Action parameter in Caliburn Micro?
KBoek
  • 5,794
  • 5
  • 32
  • 49

2 Answers2

24

If you put single quotes around the parameter name, it will properly convert for you.

<Button Content="Close without saving"
        c:Message.Attach="Close('false')" />
<Button Content="Save and Close"
        c:Message.Attach="Close('true')" />
Adrian
  • 1,338
  • 8
  • 17
1

You can try to use interactivity + triggers:

<i:Interaction.Triggers>
            <i:EventTrigger  EventName="Click">
                <cl:ActionMessage  MethodName="MyMethod" >
                    <cl:Parameter Value="True">

                    </cl:Parameter>
                </cl:ActionMessage>
            </i:EventTrigger>
        </i:Interaction.Triggers>
Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
  • not working, my guess is that "True" is still recognized as a reference to an object or object property, not as a boolean. Any other suggestions? – KBoek Nov 07 '11 at 11:06
  • I did something here http://www.felicepollano.com/2011/05/09/DottedSyntaxInMessageParameterWithCaliburnMicro.aspx but with other purpose, but I think is a starting point to customize in order to have your case solved. – Felice Pollano Nov 07 '11 at 11:23
  • I read your blog post; it could be a solution, but it's a lot of code for something so simple as a boolean. In that case I'd rather create two separate methods like "SaveWithoutClose()" and "SaveAndClose()" – KBoek Nov 07 '11 at 11:57