Link to home
Start Free TrialLog in
Avatar of cyimxtck
cyimxtckFlag for United States of America

asked on

WPF window 1 trying to reload window 2's drop down list

In what seems so simple, I cannot get the first window to update the drop down list from a second window.

So a products window opens and all is good.  A click event opens another window to add a product should it not exist already.  Now in that second window I successfully add the product that is missing so I can select that value in the drop down list in window 1.

In window 2 I have:

var ProductsWindow = new Products();
ProductsWindow.PopulateDropDown();

When I step through the code it goes and fires everything as would be imagined.

When I click the drop down in window 1 it still doesn't have the new value.  If I close and reload window 1 the value is there.

Is there like a refresh or something in WPF 4.0 that I am missing and if so how can I instantiate this method?

Please help!  :)

Thanks in advance,

B
Avatar of Aaron Jabamani
Aaron Jabamani
Flag of United Kingdom of Great Britain and Northern Ireland image

Hope you are used ObservableCollection<T> to bind to drop down. Adding/Removing an item automatically Binds to the control.
Avatar of cyimxtck

ASKER

I am looking at how to do this now and hope that it works.
I am having problems trying to create the ObservableCollection and use it successfully.

There are thousands of pages on the web with examples but this doesn't give me the insight I am trying to get.

I have tried many things:

        private ObservableCollection<Products> products;
        public ObservableCollection<Products> Product
        {
        get { return products; }
        set { products = value;
        OnPropertyChanged(new PropertyChangedEventArgs("Products"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
            PropertyChanged(this, e);
        }


and nothing is working?

I have a dataset that I need to make the collection and when it changes, have the drop down list contian the new value.  It can only ever be a new value and only one value could appear at a time as new...

Any thoughts?
Tried this and it doesn't work either....

In the child window button click event: ProductsWindow.Handler();

In the parent window with the drop down:

        private ObservableCollection<DataSet> productcollection;

        public void Handler()
        {
            productcollection = new ObservableCollection<DataSet>();
            productcollection.CollectionChanged += HandleChange;
        }

        public void HandleChange(object sender, NotifyCollectionChangedEventArgs e)
        {
            DataSet DS = DatabaseFunction.ExecuteStoredProcDS(conn, OracleObjects.SelectExchange);

            foreach (var x in e.NewItems)
            {
                rcboCommodityExchange.Items.Add(productcollection.ToString());
            }
}
Silly Question, did you bind the collection to the drop down control ? can we see the whole source code please
Here is where it gets populated originally in the main window:

        public void ProductsOpen()
        {
            this.Show();
            PopulateDropDowns();
        }

        public void PopulateDropDowns()
        {
            PopulateExchanges();
        }

        public void PopulateExchanges()
        {
            DataSet DS = DatabaseFunction.ExecuteStoredProcDS(conn, OracleObjects.SelectExchange);

            try
            {
                foreach (DataRow DR in DS.Tables[0].Rows)
                {
                    rcboCommodityExchange.Items.Add(DR[0].ToString());
                }
            }

            catch (Exception ex)
            {
                throw ex;
            }

            finally
            {
                DS.Dispose();
            }
        }

Here are the two new pieces I have tried:

        private ObservableCollection<DataSet> productcollection;

        public void Handler()
        {
            productcollection = new ObservableCollection<DataSet>();
            productcollection.CollectionChanged += HandleChange;
        }

        public void HandleChange(object sender, NotifyCollectionChangedEventArgs e)
        {
            DataSet DS = DatabaseFunction.ExecuteStoredProcDS(conn, OracleObjects.SelectExchange);

            foreach (var x in e.NewItems)
            {
                rcboCommodityExchange.Items.Add(productcollection.ToString());
            }

            foreach (var y in e.OldItems)
            {
                //do something
            }

            if (e.Action == NotifyCollectionChangedAction.Move)
            {
                //do something
            }
        }


        private ObservableCollection<Products> products;
        public ObservableCollection<Products> Product
        {
        get { return products; }
        set { products = value;
        OnPropertyChanged(new PropertyChangedEventArgs("Products"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
            PropertyChanged(this, e);
        }

        private void PopulateProductTypes()
        {
            DataSet DS = DatabaseFunction.ExecuteStoredProcDS(conn, OracleObjects.SelectDistinctProductType);

            try
            {
                foreach (DataRow DR in DS.Tables[0].Rows)
                {
                    rcboCommodityMapping.Items.Add(DR[0].ToString());
                }
            }

            catch (Exception ex)
            {
                throw ex;
            }

            finally
            {
                DS.Dispose();
            }
        }


Here is the child window and its insert:

        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            Cleanup();
            var ProductsWindow = new Products();

            try
            {
                if (tbxExchange.Text.Length > 0)
                {
                    if (ExistingExchanges() == false)
                    {
                        StoredProcParameter[] oPL = ExchangeName();
                        DatabaseFunction.ExecuteStoredProcNonQuery(conn, OracleObjects.InsertExchange, ref oPL);

                        ProductsWindow.Handler();

                        tbxStatus.Text = "Exchange entered successfully.";
                    }

                    else
                    {
                        tbxStatus.Text = "That Exchange already exists.";
                    }
                }

                else
                {
                    tbxStatus.Text = "Please enter an Exchange.";
                }
            }

            catch (OracleException OE)
            {
                tbxStatus.Text = OE.Errors.ToString();
            }
        }


 ***** Note: I walk through this code and cannot get it to hit the HandleChange method in the parent window even though I have it called out in the Handler method as:

productcollection.CollectionChanged += HandleChange;


        public void Handler()
        {
            productcollection = new ObservableCollection<DataSet>();
            productcollection.CollectionChanged += HandleChange;
        }

        public void HandleChange(object sender, NotifyCollectionChangedEventArgs e)


If I am missing something trivial please forgive me as I have tweaked this code 100 times at least.

Thanks,
B
ASKER CERTIFIED SOLUTION
Avatar of Aaron Jabamani
Aaron Jabamani
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial