Can I set a DataContext to a static class?

牧云@^-^@ 提交于 2019-11-30 19:57:06

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}}" /> 
  • I think that's exactly for what I'm looking. – Will Jan 10 '15 at 20:30
7

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.

  • Okay, right, I used the incorrect vernacular. I don't want any instance of the class, at all. I just want a structure through which I can access this information. Edited... – Will Jan 10 '15 at 19:51
  • @Will: Well then you can't set a DataContext property to be that. I suspect you really do want something with a single instance - see my edit for how to do that. – Jon Skeet Jan 10 '15 at 19:52
  • Then if I implemented it in this fashion I would want no static properties within the class itself, correct? – Will Jan 10 '15 at 19:59
  • @Will: That's right - they'd be instance properties instead. – Jon Skeet Jan 10 '15 at 20:00
  • I get this error : The type <class> does not include any accessible constructors. – Will Jan 10 '15 at 20:04
0

This syntax also works for databinding to static properties without generating the error (and I have no idea why):

DataContext={Binding Path=(prefix:TypeName.StaticPropertyName)}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!