?
Solved

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

Posted on 2012-09-18
7
Medium Priority
?
584 Views
Last Modified: 2012-09-24
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
0
Comment
Question by:cyimxtck
  • 4
  • 3
7 Comments
 
LVL 25

Expert Comment

by:apeter
ID: 38415623
Hope you are used ObservableCollection<T> to bind to drop down. Adding/Removing an item automatically Binds to the control.
0
 

Author Comment

by:cyimxtck
ID: 38417542
I am looking at how to do this now and hope that it works.
0
 

Author Comment

by:cyimxtck
ID: 38417985
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?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cyimxtck
ID: 38418142
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());
            }
}
0
 
LVL 25

Expert Comment

by:apeter
ID: 38419355
Silly Question, did you bind the collection to the drop down control ? can we see the whole source code please
0
 

Author Comment

by:cyimxtck
ID: 38419423
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
0
 
LVL 25

Accepted Solution

by:
apeter earned 2000 total points
ID: 38426907
Form a list as given below in your datarow loop

public ObservableCollection<string> list = new ObservableCollection<string>();
list.Add("a");
list.Add("b");
list.Add("c");
this.cbx.ItemsSource = list;
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question