Caliburn.Micro:一个按钮已经绑定按照约定的方法绑定文本 [英] Caliburn.Micro: Bind text of a button that is already bound by convention to a method
问题描述
我使用Caliburn.Micro出色的公约为基础的结合一键绑定到我的视图模型的方法。事情是,我希望按钮的内容
绑定到一个字符串
视图模型的属性。
目前,我允许约定的方式结合,并明确约束力的内容。这里有一个例子:
例如:
<按钮X:名称=提交CONTENT ={结合SubmitCaption}/>
公共类MyViewModel:PropertyChangedBase
{
公共无效提交(){}
公共字符串SubmitCaption {获得;组; } //技术上将提高PropertyChanged事件
}
不过,我在想,如果有有这样做的更优雅的方式?我在考虑覆盖的按钮约定,以便它仍然绑定按钮操作的方法,但内容到指定的按约定财产。
<按钮X:名称=提交/>
公共类MyViewModel:PropertyChangedBase
{
公共无效提交(){}
公共字符串SubmitContent {获得;组; } //技术上将提高PropertyChanged事件
}
您可以自定义所有按键的默认行为,但我认为你目前的方法更明确,明显给开发商,而似乎没有特别讨厌的。我会建议与你有什么坚持。
如果你决定要在按钮
的内容要多字符串会发生什么?你让 SubmitContent
的对象类型?你会设置它在code值?这听起来很厉害了我。
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
}
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.
这篇关于Caliburn.Micro:一个按钮已经绑定按照约定的方法绑定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!