2009-05-03 87 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对象和里面的一个结合?