Can I set a DataContext to a static class?
You can bind to a static field or property by using the {x:Static} binding syntax.
x:Static is used to get static fields and properties. You can set the datacontext to a static field, or property, but not to a static type.
Example below:
<DataContext Source="{x:Static prefix:typeName.staticMemberName}" />
It'd be more appropriate for you to ignore the datacontext, and just use a {x:Static binding for each value you wish to bind. For example, below would bind the program name static property:
<TextBlock Text="{Binding Source={x:Static pi:ProgramInfo.ProgramName}}" />
From the original version of the question:
I've declared it static since I will only ever need a single application-wide accessible instance of the class.
That's not what a static class is. You can never have any instance of a static class.
Although you've now changed the question to refer to there being no instances, a single instance really is probably a better idea, as binding via a data context is more geared up for instances.
What you're probably looking for is a singleton - where you can create an instance, and most of the members are instance members, but where there's guaranteed to only be a single instance.
You can make a singleton very easily:
public sealed class Singleton
{
private static readonly Singleton instance = new Singleton();
public static Singleton Instance { get { return instance; } }
// Private constructor to prevent instantiation
private Singleton() {}
// Instance members from here onwards
}
There are various other implementation options, mind you - see my page on the topic for more examples.
Then you'd set the DataContext
to refer to the singleton instance. (I assume that's easy enough in WPF - it's been too long since I've done it.)
Without that single instance, the only thing you could potentially set your DataContext
to would be the type itself - and unless WPF is set up to specifically know to fetch the static members of the type which is being referenced when the context is set to a type, I can't see it working.