Is it possible to set a DataContext in a static class?

I have a static class that reads information from an application assembly.

I declared it static, since the class does not need to declare an instance and will only ever be read directly from the application. I have a control with several shortcuts that I would like to use to display some information.

How can I set DataContext parameters equal to class?

the code:

/// <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> 
+6
source share
3 answers

You can bind to a static field or property using the {x: Static} binding syntax.

x: Static is used to get static fields and properties. You can set the datacontext for a static field or property, but not for a static type.

Example below:

 <DataContext Source="{x:Static prefix:typeName.staticMemberName}" /> 

It would be more appropriate to ignore the datacontext and just use the {x: Static binding for each value you want to bind. For example, the static property of the program name will be attached below:

 <TextBlock Text="{Binding Source={x:Static pi:ProgramInfo.ProgramName}}" /> 
+12
source

From the original version of the question:

I declared it static, since I will need only one available instance of the class for the entire application.

This is not what a static class is. You can never have an instance of a static class.

Although you have now modified the question to refer to the lack of instances, a single instance is indeed probably the best idea, since binding through the data context is more instance oriented.

What you're probably looking for is a singleton where you can instantiate, and most members are members of the instance, but where there is guaranteed to be only one instance.

You can make singleton very easy:

 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 implementation options, of course - see my related page for more examples.

Then you must set the DataContext to reference the singleton instance. (I assume this is easy enough in WPF - it has been too long since I did it.)

Without this single instance, the only thing you could set for your DataContext would be the type itself - and if WPF is not configured specifically to retrieve the static members of the type referenced when the context is set to the type, I don't see it working.

+7
source

This syntax also works to bind data to static properties without generating an error (and I have no idea why):

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

0
source

Source: https://habr.com/ru/post/1210813/


All Articles