Sometimes WPF can sometimes be infuriating.
I have a fairly simple application consisting of one main window containing a tab control and several tabs. I did not like the idea of ββhaving code for all tabs in a single file, so I used the answer from this question to split each tab into a separate user control.
In my main window, I have an instance of an object that contains application parameters and some other application data. Some of my tabs require access to this data for data binding purposes. I could not find a good way to achieve this.
First, I tried to access the parent window in the Loaded controls and get a link to the property in the main window that opened the settings object, as shown in the code below. This type of work, with the exception of the Loaded event, is triggered every time a tab receives focus. In addition, this event occurs at the end of the control life cycle, so I cannot bind to any properties of this object in the XAML user control.
private void MyUserControl_Loaded(object sender, RoutedEventArgs e) { this.ApplicationSettings = ((MainWindow)Window.GetWindow(this)).ApplicationSettings; }
Then I experimented with passing data to the custom control constructor, but this does not exist in XAML.
Given that these are the parameters of the entire application, I could make the SingleSeries ApplicationSettings class and refer to it everywhere, but I would prefer not to do this to test the modules.
So how can this be done? Is my approach fundamentally wrong? In my opinion, all these user interface elements are part of the same window and therefore must have access to data from the main window, but the object model does not seem to allow this.
source share