Question

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"; }
                }.....
Was it helpful?

Solution


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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top