我有一个静态类,它从应用程序组件中读取信息。
我将其声明为静态,因为该类不需要实例声明,并且将直接从整个应用程序中读取。我有一个带有几个标签的控件,我想使用它来显示其中的一些信息。
我应该如何设置控件的DataContext等于该类?
代码:
/// <summary>
/// Class for Reading Program Information.
/// </summary>
public static class ProgramInfo {
private static Assembly ProgramAssembly = Assembly.GetEntryAssembly( );
/// <summary>
/// Get Program Name
/// </summary>
public static string ProgramName {
get { return ProgramInfo.ProgramAssembly.GetCustomAttribute<AssemblyProductAttribute>( ).Product; }
}
/// <summary>
/// Get Program Build Date
/// </summary>
public static DateTime BuildDate {
get { return File.GetLastWriteTime( ProgramInfo.ProgramAssembly.Location ); }
}
/// <summary>
/// Get Program Version (Major.Minor)
/// </summary>
public static string Version {
get {
System.Version V = ProgramInfo.ProgramAssembly.GetName( ).Version;
return V.Major.ToString( ) + '.' + V.Minor.ToString( );
}
}
/// <summary>
/// Get Program Build (Build.Revision)
/// </summary>
public static string Build {
get {
System.Version V = ProgramInfo.ProgramAssembly.GetName( ).Version;
return V.Build.ToString( ) + '.' + V.Revision.ToString( );
}
}
}
XAML:
<UserControl x:Class="Tools.Controls.ProgramInformation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:pi="clr-namespace:Tools.Controls">
<UserControl.DataContext>
<pi:ProgramInfo/>
</UserControl.DataContext>
<!--Other Irrelevant Code-->
</UserControl>