Xamarin.Forms MVVM - 有没有办法取消绑定?

时间:2016-06-30 21:07:34

标签: mvvm data-binding xamarin xamarin.forms

有没有办法以编程方式取消设置绑定?类似的东西:

myLabel.UnsetBinding(Label.TextColorProperty);

当然有必要这样做吗?

2 个答案:

答案 0 :(得分:9)

您正在寻找RemoveBinding()方法:https://developer.xamarin.com/api/member/Xamarin.Forms.BindableObject.RemoveBinding/p/Xamarin.Forms.BindableProperty/

对于你的例子:

myLabel.RemoveBinding(Label.TextColorProperty);

答案 1 :(得分:3)

Binding附加到BindableProperty,而不是类。

您可以将Property设置为其他任何内容,Binding将会消失;

<form *ngIf="email" class="form-horizontal">
    <div class="form-group">
        <label for="inputBudgetCenter" class="col-sm-2 control-label">Budget Center</label>
        <div class="col-sm-10">
            <input [(ngModel)]="email.BudgetCenter" type="text" class="form-control" id="inputBudgetCenter" placeholder="Example: B001">
        </div>
    </div>
    <div class="form-group">
        <label for="inputEmail" class="col-sm-2 control-label">Email</label>
        <div class="col-sm-10">
            <input [(ngModel)]="email.EmailAddress" type="text" class="form-control" id="inputEmail" placeholder="Example: joe@clemson.edu">
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button (click)="updateRow()" type="submit" class="btn btn-default">Save</button>
        </div>
    </div>
</form>