1

I'm working on a WPF application in Visual Studio 2010.

When application windows are loaded into the designer, attempts to retrieve connection strings or app settings always return null. E.g., if I have an app setting called "foo" with value "bar", in the designer, ConnectionManager.AppSettings["foo"] will return null, while at runtime, it will return "bar". This is causing me some consternation, since my window is now throwing exceptions in the designer.

Is this a known bug (or "feature") in Visual Studio? I haven't been able to find mention of it elsewhere.

If any of you have encountered this before, is there a common workaround available?

EDIT, per Phil's request:

This can be trivially replicated by placing a label in a Window, à la:

 <Label Content="{Binding TheText}" HorizontalAlignment="Left" />

And in the viewmodel, setting the TheText element to the value of the application setting "foo":

TheText = ConfigurationManager.AppSettings["foo"];

The app.config contains the following appSettings section:

<appSettings>
    <add key="foo" value="bar"/>
</appSettings>

EDIT: I've marked Phil's solution as the correct one, since it seems like using the settings API is the only way around this. Sadly, it's not a viable workaround in all cases. I've raised an issue on MS Connect about this: https://connect.microsoft.com/VisualStudio/feedback/details/738316/system-configuration-configurationmanager-does-not-work-in-wpf-designer

ngroot
  • 1,186
  • 5
  • 11

1 Answers1

1

I have tried something similar to your example with no problem:

1) added string setting in project properties Settings tab

2) added control with label

<Label Content="{Binding TheText}" HorizontalAlignment="Left" />

3) added view model

public class ViewModel
{
    public string TheText{ get{ return Settings.Default.TheSetting; } }
}

4) Data context

<UserControl.DataContext>
    <ViewModel/>
</UserControl.DataContext>

The main difference is I'm using typed App settings through Settings.Default.

If your app.config contains settings in this style:

  <applicationSettings>
    <MyTestApp.Properties.Settings>
      <setting name="TheSetting" serializeAs="String">
        <value>Test</value>
      </setting>
    </MyTestApp.Properties.Settings>
  </applicationSettings>

You need to use Settings.Default.xxx

Phil
  • 42,255
  • 9
  • 100
  • 100