2016-02-15 30 views
0

我想通過下面的代碼來控制背景顏色,但顏色不會像我想要的那樣變成黑色。試圖從標籤中控制背景顏色,當我將它添加到列表中

我在我的列表中加載我的公共顏色爲透明,當它發現我想讓它變成黑色但似乎不起作用。任何我出錯的線索?

XAML:

<Label BackgroundColor="{Binding thebackgroundColor}" /> 

CODE:

public class pubClass 
{ 

public Color thebackgroundColor { get; set; } 

} 

async void loadOurItem() 
{ 
ourList.Add (new pubClass() 
       { 
        thebackgroundColor = Color.Transparent 

       }); 
} 


protected override void OnAppearing() 
    { 
      loadOurItem(); 
      var theClass = new pubClass(); 
      if (theClass.thebackgroundColor != null) { 

       theClass.thebackgroundColor = Color.Black; 
      } 

     } 
    } 

更新:

private void NotifyPropertyChanged(theClass.thebackgroundColor) 
{ 
    if (PropertyChanged != null) 
    { 
     PropertyChanged(this, new PropertyChangedEventArgs(theClass.thebackgroundColor = Color.Black)); 
    } 
} 
+0

pubClass需要實現INotifyPropert yChanged – Jason

+0

檢查我更新的代碼,類似的東西?並從OnAppearing中刪除它? –

+0

還有更多。請參閱https://developer.xamarin.com/guides/xamarin-forms/user-interface/xaml-basics/data_bindings_to_mvvm/。此外,您發佈的代碼不會顯示您爲頁面設置BindingContext的位置。最後,你好像從多個文件發佈片段,這會使你的代碼難以閱讀。請明確指出每段代碼的來源。 – Jason

回答

1

這聽起來像你想從調整模型更新ListView項目雖然你被卡住一路上有點困惑?

請嘗試下面的示例,這是一個簡單的示例,它將創建您的MyPubClassObservableCollection

屏幕底部的按鈕將顯示如何通過修改每個項目直接更新模型,並將這些更改反映在ListView上。

在此示例中,每個項目只循環顏色RedGreenBlue

MyViewCell類,這就是你將最有可能創造XAML類似的東西,通過模型的一個實現BindablePropertyTheBackgroundColor屬性綁定到LabelsBackgroundColor財產。

實施例: -

StackLayout objStackLayout = new StackLayout() 
{ 
    Orientation = StackOrientation.Vertical, 
}; 

ListView objListView = new ListView(); 
objStackLayout.Children.Add(objListView); 

objListView.ItemTemplate = new DataTemplate(typeof(MyViewCell)); 

ObservableCollection<MyPubClass> objItems = new ObservableCollection<MyPubClass>(); 
objListView.ItemsSource = objItems; 

objItems.Add(new MyPubClass(Color.Red)); 
objItems.Add(new MyPubClass(Color.Green)); 
objItems.Add(new MyPubClass(Color.Blue)); 

Button objButton1 = new Button() 
{ 
    Text = "Change Colors" 
}; 
objButton1.Clicked+=((o2,e2)=> 
{ 
    foreach (MyPubClass objItem in objItems) 
    { 
     if (objItem.TheBackgroundColor == Color.Red) 
     { 
      objItem.TheBackgroundColor = Color.Green; 
     } 
     else if (objItem.TheBackgroundColor == Color.Green) 
     { 
      objItem.TheBackgroundColor = Color.Blue; 
     } 
     else if (objItem.TheBackgroundColor == Color.Blue) 
     { 
      objItem.TheBackgroundColor = Color.Red; 
     } 
    } 
}); 
objStackLayout.Children.Add(objButton1); 

定製ViewCell: -

public class MyViewCell 
    : ViewCell 
{ 
    public MyViewCell() 
    { 
     Label objLabel = new Label(); 
     objLabel.Text = "Hello"; 

     objLabel.SetBinding(Label.BackgroundColorProperty, "TheBackgroundColor"); 

     this.View = objLabel; 
    } 
} 

支持級別: -

public class MyPubClass 
    : Xamarin.Forms.View 
{ 


    public static readonly BindableProperty TheBackgroundColorProperty = BindableProperty.Create<MyPubClass, Color>(p => p.TheBackgroundColor, default(Color)); 

    public Color TheBackgroundColor 
    { 
     get { return (Color)GetValue(TheBackgroundColorProperty); } 
     set { SetValue(TheBackgroundColorProperty, value); } 
    } 


    public MyPubClass(Color pobjColor) 
    { 
     this.TheBackgroundColor = pobjColor; 
    } 

} 
+0

不是我正在尋找的東西,但是這會明確地幫助我。謝謝! –