73

I'm facing some issues with a WPF binding to a static property. In my application there is a class called Globals which contains a static property to ThisAddIn. This object allows me to access a User property which basically is what I want to use in my Binding. So, my XAML looks like this:

<TextBlock Text="{Binding Path=User.Name
                   , Source={x:Static Member=s:Globals.ThisAddIn}}" />

The namespace s is declared at the top by the following line:

xmlns:s="clr-namespace:ConsoleApplication16.Model"

I found a ton of different approaches of how to refer to the static Property in XAML, but none of them worked except for this one, which also seems the most logical for me. Some samples I found, did not use Path or Member at all.

After some testing I found the right way to do this, is using "x:Static Member" to define the class and the static property you want to use, while Path defines the correct Properties inside this object, just like a normal binding would do.

Although this solution compiles without any complains, a XAMLParseException pops up directly telling me that the StaticExtension value cannot be resolved to an enumeration, static field, or static property.

JiBéDoublevé
  • 4,124
  • 4
  • 36
  • 57
Roper
  • 903
  • 1
  • 9
  • 17
  • 3
    How exactly is that the `right way` if it throws an exception? – H.B. Mar 19 '12 at 16:09
  • Also you should be able to drop the `Path=` and `Member=` because that will invoke the respective contructors which set the relevant properties. – H.B. Mar 19 '12 at 16:10
  • I usually bind to static classes/properties using `{Binding Source={x:Static local:MyStaticClass.SomeStaticProperty.PropertyName}`. Just be sure your properties have `get`/`set` assessors. For example, be sure you use `public string PropertyName { get; set; }` instead of `public string PropertyName;` – Rachel Mar 19 '12 at 16:33
  • @Rachel: That surely won't work as `x:Static` has a fixed syntax of `ns:Class.Property`. – H.B. Mar 19 '12 at 16:36
  • @H.B. You're right, I'm not sure why I put the `PropertyName` in there. `{Binding PropertyName, Source={x:Static local:MyStaticClass.SomeStaticProperty}` – Rachel Mar 19 '12 at 16:48
  • What do the classes look like? The XAML looks fine. – H.B. Mar 19 '12 at 16:51
  • "none of them worked except for this one ... Path defines the correct Properties inside this object" -- Sorry, but this is nonsense. StaticExtension doesn't have a Path property, and leaving off Member= works because then the value you provide is an argument to the constructor of StaticExtension, which just sets Member to it. It helps to read the documentation, rather than just trying random things. "compiles without any complains, a XAMLParseException pops up directly" -- you've contradicted yourself. – Jim Balter May 03 '16 at 02:50

3 Answers3

146

Ensure Globals.ThisAddIn is public.

You may also get this if you are using a resource file in which case you also need to ensure the access modifier is set to Public:

enter image description here

Ben
  • 54,723
  • 49
  • 178
  • 224
Chuck Rostance
  • 6,976
  • 2
  • 25
  • 17
  • 10
    That's it! It didn't make sense that at design time the property value is set correctly then at run time it fails with this exception. Very confusing. – Steztric Apr 28 '15 at 10:00
  • 1
    Holy moly, you’re right! I’d never have dreamed that the code designer would be able to access my resources at design time while compilation would fail with "ExtensionValue cannot be resolved"—same as what @Steztric writes. Many thanks! – Informagic Oct 14 '16 at 11:31
  • 3
    I came here because my resx file had internal modifier. Change it to public and it works And same, at design time it worked, veeeery confusing – Krzysztof Skowronek Nov 19 '16 at 11:48
41

In output library project, you should ensure that the Resources.resx file's generator has been modified as PublicResXFileCodeGenerator instead of ResXFileGodeGenerator by default.

Replace following with...

Not public resource

This one

correct public resource

AZ_
  • 21,688
  • 25
  • 143
  • 191
Dennis Zhang
  • 411
  • 4
  • 4
0

It may also happen that, although everything of the above was considered and correct (especially setting the Access Modifier to public), during development you add resources, e.g. a translated string, and that new resource throws the exception, while others from the same resource are working fine. That happened to me pretty often.

In this case it may help to 'Clean' the respective project and 'Rebuild' it. The 'Clean' is the matchmaker! Right-click on the project and select 'Clean' from the context menu. Than 'Rebuild' or 'Build'.

This worked for me almost every time.

Roman
  • 503
  • 6
  • 10