Search code examples
c#wpfdata-bindingstaticdatacontext

How to set a static class to Data Context?


I have a solution with 4 projects.

In the UI project, I have this XAML:

<Controls:MetroWindow x:Class="QuaverUI.Inicio.Principal"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:behaviours="clr-namespace:MahApps.Metro.Behaviours;assembly=MahApps.Metro"
    xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:QuaverUI.Inicio"    
    xmlns:common="clr-namespace:Common;assembly=Common"
    mc:Ignorable="d"
    BorderThickness="0" 
    GlowBrush="Black"
    Title="Quaver" Height="700" Width="1250" 
    WindowStartupLocation="CenterScreen"Closing="MetroWindow_Closing">

    <Window.DataContext>
        <common:Config/>
    </Window.DataContext>

</Controls:MetroWindow>

As you can see, in the XAML I use the class common:Config as DataContext, and it is a "normal" class, but I want to use the following one instead:

public static class Sesion
{
    public static int idSesion;
    public static string usuarioSesion;
    public static string rolSesion;
    public static string nombres;
    public static string primerApellido;
    public static string segundoApellido;

    public static string GetNombreCompleto()
    {
        return string.Format("{0} {1} {2}",
            nombres, primerApellido, segundoApellido);
    }
}

It's a static class, and it seems that I can't bind it to the UI.

What can I do? What do I have to put in the Binding of the Labels?


Solution

  • You have 2 solutions:

    1) Keep Sesion static and implement StaticPropertyChanged like @elgonzo suggested;

    2) Make Sesion a singleton. This is a better solution because you have more control on the Sesion lifetime. Also, you can use Dependency Injection and other useful patterns that work only with class instances (for example the DataContext property), not static classes.

    Singleton code snippet:

    public class Sesion
    {
        static Sesion()
        {
            Instance = new Sesion();
        }
    
        public static Sesion Instance { get; }
    
        private Sesion() { }
    
        public int IdSesion { get; set; }
        //other properties...
    }
    

    And you can then bind the DataContext in the MetroWindow.xaml.cs:

    public partial class MetroWindow : Window
    {
        public MetroWindow()
        {
            InitializeComponents();
    
            DataContext = Sesion.Instance;
        }
    }
    

    Note also that a Binding work only with source PROPERTIES, not public fields or methods or whatever. And, if you need to listen to the changes in the Sesion class, you'll need to make Sesion to implement INotifyPropertyChanged interface.