How can I use Caliburn.Micro conventions to set a button's text and its action?

1.4k Views Asked by At

If I have a button in my View named, say, Save, then I can add a Save property to my ViewModel, and Caliburn.Micro will automatically bind it to my button's Content. For example:

public string Save { get { return StringResources.Save; } }

Or I can add a Save method to my ViewModel, and Caliburn.Micro will execute that method when the button is clicked. For example:

public void Save() {
    Document.Save();
}

But what if I want to do both? C# doesn't let me declare a method and a property with the same name. Can I use conventions to both set the button's Content and the action to perform when it's clicked?

(I know I can manually bind one or the other, but I'd rather use conventions if it's practical.)

This is a common need, so you'd think it would be built into Caliburn.Micro, but it doesn't seem to be. I've seen some code that extends the conventions to support this (and I'll post it as an answer if nothing better comes along), but it's a workaround with some bizarre quirks -- so I'd like to hear if anyone else has made this work more cleanly.

Note: I did see this similar question, but it seems to be about whether this is a good idea or not; I'm asking about the mechanics. (I'll reserve judgment on whether it's a good idea until I've seen the mechanics. (grin))

1

There are 1 best solutions below

0
On

Quick and dirty

<Button x:Name="Save"><TextBlock x:Name="SaveText"></TextBlock></Button>