Binding 向Xamarin.forms中的绑定值添加文本
我的模型视图:Binding 向Xamarin.forms中的绑定值添加文本,binding,xamarin.forms,Binding,Xamarin.forms,我的模型视图: public string Status { get { return _status; } set { if (value == _status) { return; } _status = value; OnPropertyChanged ("Status"); } 我的看法是: Label labelStatus = new Label { TextC
public string Status {
get { return _status; }
set {
if (value == _status) {
return;
}
_status = value;
OnPropertyChanged ("Status");
}
我的看法是:
Label labelStatus = new Label {
TextColor = Color.Green,
FontSize = 20d
};
labelStatus.SetBinding (Label.TextProperty, "Status");
然后,我想用如下方式表示状态:
string presentStatus = string.Format("Your status is {0}...", labelStatus);
Label yourStatus = new Label{Text=presentStatus}
但这并没有真正起作用。使用
string presentStatus = string.Format("Your status is {0}...", SetBinding(Label.TextProperty,"Status"));
那么,在视图中向用户显示绑定值之前,我应该如何添加更多文本的绑定值呢
如果使用我没有使用的XAML,那么根据以下内容,这似乎是可能的:Xamarin表单绑定实现目前不允许在静态文本中嵌入绑定文本这样的复杂绑定场景 有两种选择 a。使用多个标签-一个带有静态文本,一个带有绑定文本 b。在ViewModel上使用连接文本的属性
public string StatusText
{
get
{
return string.Format("Your status is {0}...", Status);
}
}
public string Status {
get { return _status; }
set {
if (value == _status) {
return;
}
_status = value;
OnPropertyChanged ("Status");
OnPropertyChanged ("StatusText");
}
您可以在BindingContextChanged事件中执行此操作:
您是否在任何地方设置BindingContext?是的,仅显示labelstatus就可以了,但是当我尝试在其周围添加文本时,它失败了。
labelStatus.BindingContextChanged += (sender, e) =>
{
// Here you can change the Text dynamically
// E.G. labelStatus.text = "Title: " + labelStatus.text
};