Switch Cell的代码中的Xamarin.Forms DataBinding

时间:2014-09-23 02:17:05

标签: c# data-binding xamarin.forms

我有以下对象:

public class Notification : INotifyPropertyChanged
{
    private bool _trafficNot;
    public bool TrafficNot 
    {
        get { return _trafficNot; }
        set {
                if (value.Equals(_trafficNot))
                    return;
                _trafficNot = value;
                OnPropertyChanged();

            } 
    }

    private bool _newsNot;
    public bool NewsNot
    {
        get { return _newsNot; }
        set
        {
            if (value.Equals(_newsNot))
                return;
            _newsNot = value;
            OnPropertyChanged();
        }
    }

   public event PropertyChangedEventHandler PropertyChanged;

    void OnPropertyChanged([CallerMemberName]String propertyName=null)
    {
        var handler=PropertyChanged;
        if(handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

我从这样的对象获取数据:  //根据数据库中存储的内容设置通知对象         通知通知=新通知         {

        TrafficNot = uInfo.NotificationTraffic,
        NewsNot = uInfo.NotificationNews
    };

我希望将数据绑定到这些切换单元

    TableView tableView = new TableView
    {
        BindingContext = notification,
        Intent = TableIntent.Form,
        Root = new TableRoot
        {
            new TableSection
            {
                new SwitchCell
                {
                    Text = "News",
                    BindingContext = "NewsNot"

                },
                new SwitchCell
                {
                    Text = "Traffic",
                    BindingContext = "TrafficNot"
                },
                new SwitchCell
           }
        }
    };

我还需要做些什么来绑定它?

干杯

1 个答案:

答案 0 :(得分:7)

您根本没有绑定视图属性。您应该绑定那些Text属性,而不是将文本分配给BindingContxt和Text属性,即:

var sc = new SwitchCell();
sc.SetBinding(SwitchCell.TextProperty, new Binding("NewsNot"));

BindingContext是绑定其属性时的源对象。另请参阅DataBinding docs