19

Hy,

In my MainWindow.xaml.cs file I made a getter to get the reference to my listbox.

public ListBox LoggerList
{
    get { return Logger; }
}    

Now I want to access the LoggerList from a normal class but I don't work. I tried the following:

MainWindow parentWindow = Window.GetWindow(this) as MainWindow;
object selectedItem = parentWindow.LoggerList;

But this only works in a *xaml.cs file and not in a normal *.cs file.

Best regards

user2644964
  • 763
  • 2
  • 10
  • 28
  • A WPF ListBox is bindable, instead of having a property to your listbox you should have a property to your source containing your items, and bind them to the listbox (LoggerList.ItemSource = myList; Then you will be able to bind your classes to multiple windows and so you will be able to use them. The way your are handling it now is just not the right way. You should look into binding and basic WPF examples. – woutervs Apr 04 '14 at 07:55
  • 3
    Try this: `Window parentWindow = Application.Current.MainWindow`. – Anatoliy Nikolaev Apr 04 '14 at 07:56
  • Hy, tried it with Window parentWindow = Application.Current.MainWindow. But I cannot get the LoggerList. – user2644964 Apr 04 '14 at 08:05
  • I need this because I want to autoscroll the lsit box after added a element with: LoggerList.ScrollIntoView(LoggerList.Items[LoggerList.Items.Count - 1]); – user2644964 Apr 04 '14 at 08:07

1 Answers1

29

There are a number of ways of accessing Windows in WPF. If you have several open, then you can iterate through them like this:

foreach (Window window in Application.Current.Windows) window.Close();

If you had a particular type of custom Window, you could use this:

foreach (Window window in Application.Current.Windows.OfType<YourCustomWindow>()) 
    ((YourCustomWindow)window).DoSomething();

If you are just after a reference to the MainWindow, then you can simply use this:

Window mainWindow = Application.Current.MainWindow;

However, using this method, there is a chance that it will return null. In this case, make sure that you set the MainWindow to this property in it's constructor:

// From inside MainWindow.xaml.cs
Application.Current.MainWindow = this;

It should be noted however, that @woutervs is correct... you should not be accessing UI controls from Windows in library classes. You really should data bind collections to the ListBox.ItemsSource and then manipulate the data collection instead.


UPDATE >>

I don't know why your Application.Current object is null... it could be because you've loaded your class library into a different AppDomain. Either way, I think that you are missing the big picture. There really is no reason why a class library class should need a reference to the main Window.

If you need to perform some work on the data collection, then just pass the data collection from code behind, or your view model. Once the work is complete, then just pass it back to the UI where you have access to the ListBox and/or the collection that is data bound to the ItemsSource property.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • 1
    Hy, tried it with Window parentWindow = Application.Current.MainWindow. But I cannot get the LoggerList. I need this because I want to autoscroll the list box after added a element with: LoggerList.ScrollIntoView(LoggerList.Items[LoggerList.Items.Count - 1]); – user2644964 Apr 04 '14 at 08:09
  • Hy, I also set the MainWindow to this property in it's constructor. Yeah, I bind the a observable collection to the list but the listbox don't auto scroll with the binding. Did you mean that or something else? – user2644964 Apr 04 '14 at 08:14
  • You can make a `ListBox` scroll to position by setting a selected item in your `Observablecollection`... see the [How to control the scroll position of a ListBox in a MVVM WPF app](http://stackoverflow.com/questions/2292360/how-to-control-the-scroll-position-of-a-listbox-in-a-mvvm-wpf-app) question here on Stack Overflow for more information. – Sheridan Apr 04 '14 at 08:28
  • Is there no way to get a Window reference from one of its child controls? – Kyle Delaney Jun 11 '18 at 19:30
  • @KyleDelaney, you can use the [`VisualTreeHelper`](https://msdn.microsoft.com/en-us/library/system.windows.media.visualtreehelper(v=vs.110).aspx) to walk the visual tree up to the parent window. You'll need to ask a new question here to get further information, or you can find many examples online. – Sheridan Jun 12 '18 at 13:52
  • @Sheridan Actually it looks like `Window.GetWindow()` does the job. Thanks! – Kyle Delaney Jun 12 '18 at 15:02
  • Also `Application.Current.MainWindow`. ;) – Sheridan Jun 13 '18 at 08:33