Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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))

like image 309
Joe White Avatar asked Oct 16 '25 15:10

Joe White


1 Answers

Quick and dirty

<Button x:Name="Save"><TextBlock x:Name="SaveText"></TextBlock></Button>
like image 132
Anders Avatar answered Oct 19 '25 12:10

Anders



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!