0

I'm trying to bind via xaml an app.config value to a control. Here's the code:

//App.config:
<appSettings>
    <add key="SoundFile" value="C:\\Windows\\Media\\Windows Hardware Fail.wav"/>
</appSettings>

//window.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:properties="clr-namespace:MfClient.Properties">
...
<SoundPlayerAction Source="{Binding Source={x:Static p:Settings.Default},Path=SoundFile}"/>

This gives this error:

Error 44 Unknown build error, 'Key cannot be null. Parameter name: key Line 30 Position 56.'

If I remove the binding everything turns out to be ok:

<SoundPlayerAction Source="C:\Windows\Media\Windows Hardware Fail.wav" />

In order to bind (w/o the two way approach that I don't need), I've followed this and this other threads.

What am I getting wrong?

Community
  • 1
  • 1
misleadingTitle
  • 657
  • 6
  • 21
  • please elaborate more...where you setting alias for app.config ? – A.T. Jun 24 '13 at 08:46
  • Sorry, maybe I didn't explain myself. I was trying to keed the path of the sound in the app.config, in order to beeng able to change it after deploy. – misleadingTitle Jun 24 '13 at 08:49
  • You declare namespace prefix as `properties` and use it as `p:`. Is it copy/paste error or your code actually looks like this? – dkozl Jun 24 '13 at 08:50
  • It was a copypaste error directly in my code. Now it works! Sorry if I wasted your time. The real problem beside my distraction was the misleading error description I think. – misleadingTitle Jun 24 '13 at 08:52

1 Answers1

0

You should bind your SoundplayerAction to a ViewModel or an AppconfigProvider class you created. On that class create a Readonly property that retrieves the appconfig value.

You can add that class as a statis resource on your view.

This way you can also always easily change the location where you will get that path and combine multiple application settings into one provider class

JMan
  • 2,611
  • 3
  • 30
  • 51