-1

Loads the dataGrid and populates the Datagrid a row of 1'

public partial class MainWindow : Window
{
    public MainWindow()
    {

        InitializeComponent();
        update();
        //this.DataContext = this;



    }
    CricketEvent events = new CricketEvent();

    private void update()
    {
        events.updateList(new CricketEvent[1] { new CricketEvent(){Runs="1"} });
        DG1.ItemsSource = events.RunsList;
    }
    private void DG1_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
    {
        Window1 windowToOpen = new Window1();
        var selectedUser = this.DG1.SelectedItem;
        windowToOpen.Show();

    }
}

Main class that loads the OnPropertyChanged I have a List property and string property that calls the OnPropertyChanged but I want the individual "Runs" property to be updated on its own rather than the whole collection.

class CricketEvent : INotifyPropertyChanged

{
    private ObservableCollection<CricketEvent> runsList;
    public string runs { get; set; }


    public CricketEvent(string numofRuns) {
        this.Runs = numofRuns;
    }
    public CricketEvent() { }

    public event PropertyChangedEventHandler PropertyChanged;

    public ObservableCollection<CricketEvent> RunsList

    {
        get { return this.runsList; }

        set
        {
            if (value != this.runsList)
            {
                this.runsList = value;
                OnPropertyChanged("RunsList");
            }
        }
    }
    public string Runs
    {
        get { return runs; }
        set
        {
            runs = value;
            // Call OnPropertyChanged whenever the property is updated
            OnPropertyChanged("Runs");
        }
    }


    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }


    public ObservableCollection<CricketEvent> updateList(CricketEvent []events)
    {
        runsList = new ObservableCollection<CricketEvent>(events.ToList()); 

        return runsList;
    }

}

This is the update window that brings up a text box and should change the "1s" In the previous window to whatever is typed into the textbox

public partial class Window1 : Window
{

    public Window1()
    {


        InitializeComponent();
    }

    CricketEvent events = new CricketEvent();
    MainWindow main = new MainWindow();
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        events.updateList(new CricketEvent[1] { new CricketEvent(txt1.Text.ToString()) });

        main.DG1.ItemsSource = events.RunsList;

    }
Mark Evans
  • 45
  • 1
  • 1
  • 8
  • `events.updateList(...)` won't magically set `events.RunsList`, unless you write `events.RunsList = events.updateList(...);` – Clemens May 12 '16 at 10:07

1 Answers1

0

The Button_Click event in Window1 does not use the instance of MainWindow that is show - it creates a new Window instance (that is not shown) and adds the updated list to the DG1.ItemsSource property. To solve that, pass the original instance of Window to the created Window1 in constructor and use that.

However, you should review your update strategy (and code style) because there is potential for improvments:

  • It is not a good idea to create a new collection if you want to update just one property of one item. Observable collections provide change notification, so you dont have to recreate the collection at all.

  • Instead of assinging the collection in code behind, use databinding to bind the collection to the ItemsSource. DataBinding results in automatic update of GUI elements if the collection or one item of you collection changed.

JanDotNet
  • 3,746
  • 21
  • 30