SD7
SD7

Reputation: 514

update listbox text values dynamically

I have found the way add or delete the item in listbox which takes advantages of the Observable Collection. But i want to update all the particular binding property values in the listbox for example all the textblocks only in listbox to update. Here is the example for adding item in listbox, but i want to update the values:binding Name on button click outside listbox so how can i achieve that?

public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        FillListBox();
    }
    MovieList Mov = new MovieList();
    private void FillListBox()
    {
        listBox1.ItemsSource = Mov;
        listBox1.DisplayMemberPath = "Name";
        listBox1.SelectionChanged += new SelectionChangedEventHandler(listBox1_SelectionChanged);
    }
    void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Mov.Add(new Movie { Name = "Ghillli", Actor = "Vijay" });
    }
}
public class Movie
{
    public string Actor { get; set; }
    public string Name { get; set; }
}
public class MovieList : ObservableCollection
{
    public MovieList()
    {
        Add(new Movie { Name = "Kaavalan", Actor = "Vijay", });
        Add(new Movie { Name = "Velayutham", Actor = "Vijay", });
        Add(new Movie { Name = "7th Sense", Actor = "Surya", });
        Add(new Movie { Name = "Billa 2", Actor = "Ajith Kumar", });
        Add(new Movie { Name = "Nanban", Actor = "Vijay", });
    }
}

so how to update all values binding Name Property in listbox without nulling itemsource and rebinding all values and only updating Name property?

Upvotes: 0

Views: 204

Answers (1)

Krunal Mevada
Krunal Mevada

Reputation: 1655

Change in code as below:

public class Movie
{
    public string Actor { get; set; }
    public string Name { get; set; }
}
public class MovieList : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        var mevent = PropertyChanged;
        if (mevent != null)
        {
            mevent(this, e);
        }
    }

    ObservableCollection<Movie> Movieitems = new ObservableCollection<Movie>();
    public ObservableCollection<Movie> MovieItems
    {
        get { return Movieitems; }
        set
        {
            if (Movieitems == value)
            {
                return;
            }
            Movieitems = value;
            OnPropertyChanged(new PropertyChangedEventArgs("MovieItems"));
        }
    }

    public MovieList()
    {
        Movieitems.Add(new Movie { Name = "Kaavalan", Actor = "Vijay", });
        Movieitems.Add(new Movie { Name = "Velayutham", Actor = "Vijay", });
        Movieitems.Add(new Movie { Name = "7th Sense", Actor = "Surya", });
        Movieitems.Add(new Movie { Name = "Billa 2", Actor = "Ajith Kumar", });
        Movieitems.Add(new Movie { Name = "Nanban", Actor = "Vijay", });
    }
}

And add item dynamically in listbox in listBox1_SelectionChanged as below:

Mov.MovieItems.Add(new Movie { Name = "Ghillli", Actor = "Vijay" });

Edit:

For update existing Name property

Mov.MovieItems[SelectedIndexValue] = new Movie { Name = "Ghillli", Actor = "Vijay" };

SelectedIndexValue = 0,1,2,3... etc.

For update Name property of colletion

Mov.MovieItems[SelectedIndexValue].Name = "Ghillli";

Upvotes: 1

Related Questions