Send a boolean as an Action parameter in Caliburn Micro
Asked Answered
H

2

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?
Hellenhellene answered 7/11, 2011 at 10:35 Comment(0)
T
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')" />
Tanager answered 8/11, 2011 at 22:13 Comment(0)
C
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>
Chair answered 7/11, 2011 at 10:39 Comment(3)
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?Hellenhellene
I did something here felicepollano.com/2011/05/09/… but with other purpose, but I think is a starting point to customize in order to have your case solved.Chair
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()"Hellenhellene

© 2022 - 2024 — McMap. All rights reserved.