2012-06-01 114 views
0

我正在嘗試使ListBox根據某些更改數據更新其內容。 的XAML是如下通過數據綁定更改列表框項目的背景顏色屬性

StackPanel Orientation="Vertical"> 
<ListBox x:Name="listWatch" > 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Grid ShowGridLines="True"> 
       <Grid Grid.Column="0" Background="{Binding Path=Color"> 
        <TextBlock Text="{ Binding Path=LTP}" Padding="2 2 2 2"/> 
       </Grid> 
      </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
<Button x:Name="btn" Click="btn_Click" Content="Button" /> 

我用於表單數據strucure如下

public class WatchRow : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    string _color; 
    decimal _lTP; 

    public WatchRow(decimal LTP,string color) 
    {   
     this.LTP = LTP;   
     this.Color = color; 
    } 
    public string Color 
    { 
     get { return _color; } 
     set{ 
      _color = value; 
      OnPropertyChanged(_color); 
     } 
    }  
    public decimal LTP 
    { 
     get { return _lTP; } 
     set 
     { 
      _lTP = value; 
      OnPropertyChanged(_lTP.ToString()); 
     } 
    } 
    protected void OnPropertyChanged(string info) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(info)); 
     } 
    } 
} 

public class Watch:ObservableCollection<WatchRow> 
    { 
     public Watch():base() 
     { 
     }  
    } 

背後文件中的代碼是這樣

Watch watch = new Watch(); 
private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e) 
{ 
    watch.Add(new WatchRow(132, "black")); 
    watch.Add(new WatchRow(123, "red")); 
    listWatch.ItemsSource = watch; 
watch[0].Color = "green"; 
} 

private void btn_Click(object sender, RoutedEventArgs e) 
{ 

    watch[0].Color = "green"; 
} 

類我問題是我無法通過設置顏色來更改列表框項目的顏色財產(手錶[0] .Color =「綠色」;)in btn_Click,如代碼所示。但是相同的代碼在PhoneApplicationPage_Loaded_1中可用。我不知道我錯了什麼。有任何想法嗎?

回答

0

我相信這個問題稍微改變了你如何使用PropertyChanged。當您調用OnPropertyChanged時,請通過您正在更改的屬性的名稱,而不是值。例如:

public string Color 
    { 
     get { return _color; } 
     set{ 
      _color = value; 
      OnPropertyChanged(_color); 
     } 
    } 

應該是:

public string Color 
    { 
     get { return _color; } 
     set{ 
      _color = value; 
      OnPropertyChanged("Color"); 
     } 
    } 

此外,我不知道這必然是一個問題,但是這是我一直所創建的OnPropertyChanged功能:

相反的:

protected void OnPropertyChanged(string info) 
{ 
    PropertyChangedEventHandler handler = PropertyChanged; 
    if (handler != null) 
    { 
     handler(this, new PropertyChangedEventArgs(info)); 
    } 
} 

嘗試:

private void OnPropertyChanged(String info) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(info)); 
      } 
     } 

另外,正如Magnus Johansson提到的,定義一個畫筆,並綁定Color而不是一個字符串。所以顏色屬性將是(看到他關於這個更詳細的解釋):

private Color _color; 
public Color Color 
     { 
      get { return _color; } 
      set{ 
       _color = value; 
       OnPropertyChanged("Color"); 
      } 
     } 
+0

感謝您的回覆,幫了很多 –

0

使用MVVM可以解決你的問題: 我已經測試此代碼和它的作品。你需要在三個獨立的文件分割的代碼,像這樣:代碼

視圖模型

public class WatchViewModel 
{ 
    public ObservableCollection<WatchRow> WatchRows { get; set; } 

    public void GetWatchRows() 
    { 
     WatchRows= new ObservableCollection<WatchRow>(); 
    } 

    public void AddWatchRow(int value, string color) 
    { 
     var item=new WatchRow(value, color); 
     WatchRows.Add(item); 
    } 
} 

模型

public class WatchRow : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    string _color; 
    decimal _lTP; 

    public WatchRow(decimal LTP, string color) 
    { 
     this.LTP = LTP; 
     this.Color = color; 
    } 
    public string Color 
    { 
     get { return _color; } 
     set 
     { 
      _color = value; 
      OnPropertyChanged(_color); 
     } 
    } 
    public decimal LTP 
    { 
     get { return _lTP; } 
     set 
     { 
      _lTP = value; 
      OnPropertyChanged(_lTP.ToString()); 
     } 
    } 
    protected void OnPropertyChanged(string info) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(info)); 
     } 
    } 
} 

和視圖(XAML中的背後頁)

public partial class MainPage : PhoneApplicationPage 
{ 
    private WatchViewModel watch; 

    public MainPage() 
    { 
     InitializeComponent(); 

     watch = new WatchViewModel(); 
     watch.GetWatchRows(); 


     watch.AddWatchRow(132, "green"); 
     watch.AddWatchRow(123, "red"); 
     base.DataContext = watch; 
     listWatch.ItemsSource = watch.WatchRows; 

    } 

    private void btn_Click(object sender, RoutedEventArgs e) 
    { 
     watch.AddWatchRow(132, "pink"); 
     watch.AddWatchRow(113, "yellow"); 

    } 
} 
+0

感謝您的回覆,我一定會考慮一下Mvvm –

相關問題