IsAuthenticated in Silverlight, Revisited

In an earlier series of earlier posts, I talked about binding the IsEnabled property of links / buttons / controls to IsAuthenticated. I recently needed to revisit that code and found that I wanted to create the binding in code and I wanted to refactor the binding / authentication code so that I exists in its own class. Below you’ll find what I came up with.

Creating WPF bindings in code

Turns out it’s very simple to create a WPF binding in code, you need the property name (name path actually), the source object and Dependency property you wish to bind to;

        private bool _myBool;
        public bool MyBool
        {
            get
            {
                return MyBool;
            }
            set
            {
                _myBool = value;
                SendPropertyChanged("MyBool");
            }
        }

        public void SetupBinding()
        {
            Binding binding = new Binding("MyBool");
            binding.Source = this;
            storeImageButton.SetBinding(
                Control.IsEnabledProperty, 
                binding);
        }

 

AuthenticatedBinder

The class below will allow you to create bindings in code that bind to the IsAuthenticated property of the WebContext.Current.Authentication object. To create a binding, you can use code similar to this example;

        public EvolvedImage()
        {
            InitializeComponent();
            AuthenticatedBinder.BindIsEnabledToIsAuthenticated(
                storeImageButton);
        }

 

Here’s the source code;

using System.Windows.Controls;
using System.Windows.Data;
using ImageEvolvatron.Gui.ViewModels;

namespace ImageEvolvatron.Gui
{
    public class AuthenticatedBinder : ViewModelBase
    {
        private static readonly AuthenticatedBinder _instance = new AuthenticatedBinder();
        private static Binding _binding = CreateBinding();

        private AuthenticatedBinder()
        {
            WebContext.Current.Authentication.LoggedIn += 
                (sender, e) => SendPropertyChanged(() => IsAuthenticated);
            WebContext.Current.Authentication.LoggedOut += 
                (sender, e) => SendPropertyChanged(() => IsAuthenticated);
        }

        public static AuthenticatedBinder Instance { get { return _instance; } }

        public static Binding IsAuthenticatedBinding
        {
            get
            {
                return _binding;
            }
        }

        public bool IsAuthenticated
        {
            get
            {
                return WebContext.Current.User.IsAuthenticated;
            }
        }

        public static void BindIsEnabledToIsAuthenticated(Control control)
        {
            control.SetBinding(Control.IsEnabledProperty, IsAuthenticatedBinding);
        }

        private static Binding CreateBinding()
        {
            Binding binding = new Binding("IsAuthenticated");
            binding.Source = _instance;
            return binding;
        }
    }
}

 

Oh, and ViewModelBase looks like this;

using System;
using System.ComponentModel;
using System.Linq.Expressions;

namespace ImageEvolvatron.Gui.ViewModels
{
    public class ViewModelBase :
        INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        
        protected static string GetPropertyName<T>(Expression<Func<T>> action)
        {
            var expression = (MemberExpression)action.Body;
            return expression.Member.Name;
        }
        
        protected virtual void SendPropertyChanged<T>(Expression<Func<T>> action)
        {
            string propertyName = GetPropertyName(action);
            SendPropertyChanged(propertyName);
        }

        private void SendPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

About mfagerlund
Writes code in my sleep - and sometimes it even compiles!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: