Caliburn.Micro:将已按约定绑定的按钮文本绑定到方法

发布于 2024-12-10 21:58:24 字数 747 浏览 0 评论 0原文

我正在使用 Caliburn.Micro 出色的基于约定的绑定将按钮绑定到我的视图模型上的方法。问题是,我希望按钮的 Content 绑定到视图模型的 string 属性。

目前,我允许方法的约定绑定和内容的显式绑定。这是一个例子:

例如:

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

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

但是我想知道是否有更优雅的方法来做到这一点?我正在考虑重写按钮的约定,以便它仍然将按钮操作绑定到方法,但将内容绑定到按约定命名的属性。

<Button x:Name="Submit" />

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

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Hello爱情风 2024-12-17 21:58:24

您可以自定义所有按钮的默认行为,但我认为您当前的方法对开发人员来说更加明确和明显,并且看起来并不是特别令人讨厌。我建议坚持你所拥有的。

如果您决定希望 Button 内容不仅仅是一个字符串,会发生什么?您是否将 SubmitContent 设置为对象类型?你会在代码中设置它的值吗?这对我来说听起来更恶心。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文