2009-05-03 193 views
0

我有列表框和點擊事件我打開新的面板,其中我更改列表框的數據,更準確的圖像源。我有問題如何更新列表框有新的圖片。提前致謝。 這裏是我的代碼:Silverlight:更新列表框模板項目

<ListBox x:Name="lbNarudzbe" MouseLeftButtonUp="lbNarudzbe_MouseLeftButtonUp" HorizontalAlignment="Center" MaxHeight="600"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <StackPanel Orientation="Horizontal"> 
           <Image Margin="0,5,0,0" Width="50" Height="50" HorizontalAlignment="Center" Source="{Binding Path=Picture}" /> 
           <TextBlock HorizontalAlignment="Center" FontSize="23" Text="{Binding Path=UkupnaCijena}" Width="80"/> 
          </StackPanel> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 




public partial class Page : UserControl 
    { 
     ObservableCollection<Narudzba> narudzbe = new ObservableCollection<Narudzba>(); 

     public Page() 
     { 
      InitializeComponent(); 

      narudzbe.Add(new Narudzba()); 
      narudzbe.Add(new Narudzba()); 
      narudzbe.Add(new Narudzba()); 
      narudzbe.Add(new Narudzba()); 

      lbNarudzbe.ItemsSource = narudzbe; 

     } 





    public class Narudzba 
      { 
       //... 
       public string Picture 
       { 
        get { return "picture source"; } 
       }..... 
+0

MouseLeftButtonUp事件的代碼在哪裏? – 2009-05-03 10:50:32

回答

1


基本上,當您想要更新列表框中的圖片時,您正在更新您的Narudzba類的Picture屬性,並且由於您的Narudzba類未實現INotifyPropertyChanged接口,因此列表框無法更新圖片。

下面是一些可能有所幫助的代碼。

public class Narudzba : System.ComponentModel.INotifyPropertyChanged 
{ 
    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged; 
    void Notify(string propName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propName)); 
     } 
    } 

    string _picturesource; 

    public string Picture 
    { 
     get { return _picturesource; } 
     set 
     { 
      _picturesource = value; 
      Notify("Picture"); 
     } 
    } 

    public Narudzba(string picturesource) 
    { 
     _picturesource = picturesource; 
    } 
    } 
} 

然後lbNarudzbe_MouseLeftButtonUp事件代碼看起來應該是這樣

private void lbNarudzbe_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
    { 
     Narudzba nb = (Narudzba)lbNarudzbe.SelectedItem; 
     nb.Picture = "http://somedomain.com/images/newpicture.jpg";    
    } 

HTH。

+0

謝謝。它有很多幫助。 – user100161 2009-05-03 12:05:08

0

不知道,雖然,但你能不能有相同的列表框外的imageblock對象和裏面的一個結合?