views:

70

answers:

1

Edit 3: TLDR-Version

I have a singleton DependencyObject that I'm binding UserControls to. Some of the UserControls are in the main window, some of them appear in a separate window (the settings window). The ones that appear in the settings window do not bind correctly and do not affect the values in the singleton.


I have a singleton DependencyObject that I'm using to store application-wide settings. I've also got a settings dialog that the user (theoretically) would be able to use to control the values inside of the singleton.

The application largely deals with documents, so I need to be able to store font settings for a number of different items. So I created a DependencyObject called FontSettings, it contains dependency properties for things like font family and font size.

There is a different DependencyObject for each "section" of items I need to control separately.

So, the entire structure of nested objects looks something like:

MySettings
{
     PropertyGroup AProperties;
     PropertyGroup BProperties;
     PropertyGroup CProperties;
 }

 PropertyGroup
 {
      FontSettings FontStgs;
 }

Each PropertyGroup is a distinct DependencyObject, but they will all contain a FontSettings object.

So, because I know that I will need to display the settings controls for the FontSettings objects in many places I created a simple UserControl to modify them. I thought that I would be able to simply bind the DataContext of the UserControl to the FontSettings object that I would want it to modify and as a result, changes in the UserControl would be reflected in the MySettings object (the controls inside of the UserControl are bound to properties of FontSettings). But when I set a breakpoint on the constructor of my UserControl, the DataContext is always null after initialization.

If this isn't clear enough to be helpful, let me know and I'll post actual code.

Here's a snippet of my XAML inside of the settings window:

<local:FontSettingsControl DataContext="{Binding Source={x:Static stg:MySettings.Instance}, Path=PageHeaderSettings.CompanySettings}" />            

CompanySettings is of the FontSettings type.

TLDR Version: I can't seem to get the DataContext to bind correctly. If it did, I think the controls would correctly affect the settings.

I need the DataContext to be pointed to the CompanySettings DependencyObject.

The full path to the CompanySettings object is: MySettings.Instance.PageHeaderSettings.CompanySettings

The settings singleton: MySingleton

Edit:

After checking things out with Snoop, it appears that the DataContext isn't getting set. Is something wrong with my binding expression?

Edit 2:

It seems like my problem is arising from the fact that the UserControls I'm trying to bind to are in a window that is NOT the main window. When I placed the UserControl in the main window everything worked as expected. So, does anyone know what the problem actually is caused by (I assume it has it's roots in threading) and how I should proceed to make my settings window function?

A: 

Are you actually getting problems at runtime? The way you're testing by checking the value in the constructor isn't going to tell you anything because values from Bindings don't get set until after a control has finished initializing and is loaded. Even if you were setting a local value instead of a Binding on an instance of your control in XAML, it is going to take place after the constructor has exited (just like doing new FontSettingsControl() { DataContext = myData });). Instead try using Snoop to check your DataContext values at runtime.

John Bowen
The thing that first went wrong was that the UserControl (FontSettingsControl) has a ComboBox that is bound to a property called FontSize, but when I change the value in the ComboBox, the corresponding value in the MySettings.Instance.PageHeaderSettings.CompanySettings object doesn't get updated. I'm going to take a look at Snoop to see if I can find a more precise error location.
Anthony Compton