wpf - binding datacontext to static properties of singleton class
I found myself using a lot of individual bindings to my App class for storage of properties and this led me to a untracable stackoverflow exception. I've now decided I would move those properties to a separate singleton ApplicationInfo class but I am having some issues with the binding.
If I bind directly to a member property of my class, such as CurrentUser then it works fine. But when I try to bind a datacontext to this class I get compiler errors and I am sure there is some simple modification that i've overlooked.
I've created a singleton out of this class but now when I try to compile I get the error "Unknown build error - key cannot be null" and it points to my Datacontext binding for the error message.
Here's my class:
public class ApplicationInfo
{
private ApplicationInfo()
{
}
private static ApplicationInfo _Current = new ApplicationInfo();
public static ApplicationInfo Current
{
get { return _Current; }
}
#region Application Info P开发者_JAVA技巧roperties
private static Assembly _ExecutingAssembly = System.Reflection.Assembly.GetExecutingAssembly(); //holds a copy of this app's assembly info
public static String ApplicationName
{
get { return _ExecutingAssembly.ManifestModule.Name; }
}
public static String ApplicationNameTrimmed
{
get { return _ExecutingAssembly.ManifestModule.Name.TrimEnd('.', 'e', 'x'); }
}
public static String ApplicationPath
{
get { return _ExecutingAssembly.Location; }
}
public static String ApplicationVersion
{
get { return _ExecutingAssembly.GetName().Version.ToString(); }
}
public static DateTime ApplicationCompileDate
{
get { return File.GetCreationTime(Assembly.GetExecutingAssembly().Location); }
}
#endregion
private static Boolean _hasOpenWindows;
public static Boolean HasOpenWindows
{
get { return _hasOpenWindows; }
set { _hasOpenWindows = value; }
}
private static User _currentuser;
public static User CurrentUser
{
get { return _currentuser; }
set { _currentuser = value; }
}
private static Mantissa.DAL _datalayer;
public static Mantissa.DAL DataLayer
{
get { return _datalayer; }
set { _datalayer = value; }
}
private static string _connectionstring;
public static string ConnectionString
{
get { return _connectionstring; }
set { _connectionstring = value; }
}
}
This works:
`Title="{Binding Source={x:Static my:ApplicationInfo.ApplicationNameTrimmed}}"`
This does not: (throws the key cannot be null msg)
DataContext="{Binding Source={x:Static my:ApplicationInfo.Current}}"
But when I put the same property in my App class then this works:
DataContext="{Binding Source={x:Static Application.Current}}"
so how do I fix this?
x:Static is used to get static fields and properties. ApplicationInfo is a class, not a property. To bind, you must either create an instance of it and use its instance property, or bind to a static property (accessing it doesn't require instances).
If you want to bind to a specific property, use Title="{Binding Source={x:Static my:ApplicationInfo.ApplicationNameTrimmed}}"
.
If you want to set DataContext and then to use bindings to other properties, use DataContext="{Binding Source={x:Static my:ApplicationInfo.Current}}"
and convert static properties to instance properties (remove static keyword). You'll also be able to bind like this: Title="{Binding Source={x:Static my:ApplicationInfo.Current.ApplicationNameTrimmed}}"
.
精彩评论