user100161
user100161

Reputation: 13

Silverlight: update listbox template item

I have listbox and on click event I open new panel where i change data of listbox, more accurately image source. I have problem how to update listbox to have new picture. Thanks in advance. Here is my code:

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

Upvotes: 0

Views: 3199

Answers (2)

Anand Shah
Anand Shah

Reputation: 14913


Basically when you want to update the picture in the listbox, you are updating the Picture property of your Narudzba class, and since your Narudzba class does not implement the INotifyPropertyChanged interface the listbox can't update the picture.

Here's some code that might help.

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

Then the lbNarudzbe_MouseLeftButtonUp event code should look like this

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

HTH.

Upvotes: 1

theraneman
theraneman

Reputation: 1630

Not sure though, but can you not have same binding for the imageblock object outside the listbox and the one inside it?

Upvotes: 0

Related Questions