Caliburn.Micro: Bind text of a button that is alre

2019-07-19 18:58发布

问题:

I'm using Caliburn.Micro's excellent convention-based binding to bind a button to a method on my view model. Thing is, I want the Content of the button to be bound to a string property of the view model.

At the moment I'm allowing the convention binding for the method, and an explicit binding for the content. Here's an example:

E.g.:

<Button x:Name="Submit" Content="{Binding SubmitCaption}" />

public class MyViewModel : PropertyChangedBase
{
    public void Submit() {}
    public string SubmitCaption { get; set; } // Technically would raise PropertyChanged event
}

However I was wondering if there's there a more elegant way of doing this? I'm thinking about overriding the convention for buttons so that it still binds the button action to a method but the content to a named-by-convention property.

<Button x:Name="Submit" />

public class MyViewModel : PropertyChangedBase
{
    public void Submit() {}
    public string SubmitContent { get; set; } // Technically would raise PropertyChanged event
}

回答1:

You could customise the default behaviour for all buttons, but I think your current approach is more explicit and obvious to developers, and doesn't seem particularly nasty. I would recommend sticking with what you've got.

What happens if you decide you want the Button content to be more than a string? Do you make SubmitContent an object type? Would you set its value in code? That sounds nastier to me.