Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 569041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:15:09+00:00 2026-05-13T13:15:09+00:00

In code behind file of the main window of WPF application I have a

  • 0

In code behind file of the main window of WPF application I have a method quering a database with LINQ to SQL and writing results to an ObservableCollection:

    public void GetStateByDate(string shcode)
    {
        MydbDataContext contextSts = new MydbDataContext();
        _ShAvaQuCollection.Clear();

        var sts = from p in contextSts.SAties where p.ShID == shcode select p;

        foreach (var p in sts)
            _ShAvaQuCollection.Add(new ShAvaQu
            {
                ShCode = p.ShID,
                SiID = p.SiID,
                PrCat = p.PrCat
            });
    }

When I call this method from the same code behind file (the same window), everything is OK.

If I call this method from another window, using an instanse of the main window, ObservableCollection remains empty.:

SWindow sw = new SWindow();
sw.GetStateByDate(stringpar);

What is the reason for this? Does in this case method create yet another instance of ObservableCollection?

(I can see in debugger that sw._ShAvaQuCollection contains values. Is sw._ShAvaQuCollection not the same instanse of collection as _ShAvaQuCollection? If yes, how it can be resolved?)

Edited (added)

The ObservableCollection declared this way:

    ObservableCollection<ShAvaQu> _ShAvaQuCollection =
            new ObservableCollection<ShAvaQu>();

    public ObservableCollection<ShAvaQu> ShAvaQuCollection
    { get { return _ShAvaQuCollection; } }

    public class ShAvaQu
    {
        public string ShCode { get; set; }
        public string SiID { get; set; }
        public int PrCat { get; set; }
    }

I call the method from a window, where another collection ShQuCollection displayed through ListView. In SelectionChanged event handler I take an argument for this database quering:

private void ShSelList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {           
        SWindow sw = new SWindow();         
        string str = sw.ShQuCollection[ShSelList.SelectedIndex].ShCode;
        sw.GetStateByDate(str);
        Close();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:15:09+00:00Added an answer on May 13, 2026 at 1:15 pm

    1) Most importantly you shouldn’t be calling db logic from you windows / forms. You should abstract it out into another class. Then you could have your method return a observable collection.

    However in your case I am assuming that you are trying to use the secondary form to reload / load the collection and you want it used on your primary form. The problem with this is you are creating a new instance of the form so your collection is being populated but not on your main form but a copy.

    There are a couple ways you can try to get around that.

    1) Make the method static and your observable collection static so that it updates a single instance.

    2) Pass an instance handle of your primary form into your secondary form so that you re-use the instance you already have. This would be preferable so that you are not creating new instances all over the place.

    In the constructor of the second form you could pass in the instance of your primary window so then you can use it directly. This should solve your problem.

    UPDATE: Here is some code samples. Basically there are many ways to pass a reference.

    You could do it like this with a constructor:

    // This is the constructor for your second window
    private Window _parentHandle;
    
    public SecondWindow(Window obj)
    {
        this._parentHandle = obj;
    }
    

    Then from your primary form that has the method you would open that window like this.

    SecondWindow w = new SecondWindow(this);
    w.Show();
    

    Now your second window has a direct handle to your first window so you can call your method on that variable and it will update.

    Another way is to have a public Setter method on your second window as well.

    public Window ParentContext
    {
       get { return this._parentHandle; }
       set { this._parentHandle = value; }
    }
    

    Then you could create your form instance like this:

    SecondWindow w = new SecondWindow();  // so just like normal
    w.ParentContext = this; // set the instance to the calling form
    w.Show();
    

    That is the basics. This type of scenario works in just about any scenario where you need to pass a reference.

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have WPF application with one main window . In this window there are
I have a executable jar file and i want to see code behind it.
In code-behind of an ASP.NET page I have this method: public string TestFunc() {
In my WPF app, all I have in my code-behind is the following: public
The code behind file creates a list of employees and the asp.net page loops
I had a code segment on an asp.net page in the code-behind file. I
I'm trying to create datagrid tooltip entirely in code behind file. Tooltip XAML code
i want to add file(photo) to fileupload control in the c#(code behind) thanks
I am using a base Window class in a WPF project. In the code
In visual studio i want to add a second code behind file to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.