I am really at a loss here, and have been trying to find a solution to this for several hours. I am lost. I get the following exception during an operation that was working the last time I checked.
'{DependencyProperty.UnsetValue}' is not a valid value for property 'Foreground'.
It does not take me to where the error is occuring. It takes me to a page that says “No Source Available”, and nothing else. I have tried locating the error by placing breakpoints in various places, but it seems to fail at different points during each run through. The InnerException is null.
I have seen this question, as well as various articles from Google. I cannot figure out what is going on, and I do not know how to troubleshoot from here. The Visual Studio output doesn’t appear to give any more detailed information, but I will paste it on request. Please, any help is appreciated.
I’m willing to be you have a missing resource. If you do something like:
Then you will get such an exception. We can even use a ComponentResourceKey to produce this exception:
There are few things here that cause the issue. Normally, you would get a compiler error saying a resource doesn’t exist when using
StaticResource. Such as in this case:If instead, we had done:
Then you would get a different exception (XamlParseException), saying:
With an internal exception of:
Which all leads us to the real problem (a missing resource). The reason the first two examples don’t give us a useful exception, is that we are not setting the
Foregroundproperty. We are setting theValueproperty on aSetterobject. So when the resource is not found,DependencyProperty.UnsetValueis used. Which is perfectly valid for theSetter.Valueproperty.Later, when the
Styleis applied to theButtonwe get the exception, because that’s when theDependencyProperty.UnsetValueis actually assigned to theButton.Foregroundproperty.To fix this issue, I’d search over your entire solution for
Property="Foreground"and look for any instances that use a resource that doesn’t exist.I should add that you don’t get an exception when using
DynamicResource, because in that value passed to theButton.Foregroundproperty is a “special value” (which allows deferred lookup). This “special value” won’t assign the given property unless the resource is found.