2014-09-23 24 views
2

我有以下目的: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 
    }; 

,我希望將數據綁定到這些switchells

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 
      } 
     } 
    }; 

我還需要做什麼來綁定它?

乾杯

回答

7

您根本沒有綁定視圖屬性。相反,以BindingContxt和Text屬性指定的文本,你應該綁定的Text屬性,即:

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

的BindingContext是源對象,同時要針對其屬性綁定。另見DataBinding docs

+0

啊,謝謝你糾正這個錯字,史蒂芬。 – 2014-09-24 07:19:55

+0

謝謝 - 它不讓我在TableSection中命名SwitchCell,所以我認爲它們不能被命名 - 但是由於你的回答,我努力在桌子外面宣佈。 – user1667474 2014-09-24 08:46:59