Click here to Skip to main content
15,884,298 members
Articles / Desktop Programming / WPF
Tip/Trick

WPF Custom Attached Property

Rate me:
Please Sign up or sign in to vote.
4.97/5 (18 votes)
4 Dec 2013CPOL1 min read 69.8K   1.5K   29   8
Custom attached property in WPF

Introduction

Most of the applications will have different types of users. Based on user type, they can perform operations like Create, Read, Edit, Delete, etc. so when we develop an application, we have to write multiple lines of code to enable and disable the controls for each screen based on logged in user. It is a very time consuming activity in the development phase.

Using attached property, we can simplify this activity in a generic way. This approach will be fit to WPF and Silverlight applications and we have no need to write the logic for every screen.

Sample Requirement

Application will have the below user types:

  • Super Admin
  • Admin
  • Master
  • Standard

Screen wise access

Screen Name

Action

Access to User Type

Customer

Add New Customer

SuperAdmin, Admin, Master

Customer

View Customer Personal Detail

SuperAdmin, Admin, Master, Standard

Customer

Modify Customer Detail

SuperAdmin, Admin, Master

Customer

Delete Customer Detail

SuperAdmin, Admin

Supplier

Add New Supplier

SuperAdmin,Admin,Master

Supplier

View Supplier Personal Detail

SuperAdmin, Admin, Master, Standard

Supplier

Modify Suppler Detail

SuperAdmin, Admin, Master

Supplier

Delete Supplier Detail

SuperAdmin, Admin

Manage User

Add User

SuperAdmin

Manage User

Modify User Detail

SuperAdmin

Manage User

Delete User Detail

SuperAdmin

Implementation

When we design a screen, we can set the user type to the appropriate control that can perform the action.

Sample Image - maximum width is 600 pixels

Screen Shots

When the logged in user type is “Standard”

Sample Image - maximum width is 600 pixels

When the logged in user type is “Master”

Sample Image - maximum width is 600 pixels

When the logged in user type is “Admin”

Sample Image - maximum width is 600 pixels

When the logged in user type is “Admin”

Sample Image - maximum width is 600 pixels

When the logged in user type is “Super Admin”

Sample Image - maximum width is 600 pixels

Source Code

Main Window

C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        // Need to set the user type from login screen
        AppCommon.LogedUserType = UserType.SuperAdmin;
        InitializeComponent();           
        userType.Text = "User Type : " + AppCommon.LogedUserType.ToString();
    }
}

User Types

C#
[Flags]
public enum UserType
{
    All=0,
    SuperAdmin=1,
    Admin=2,
    Master=4,
    Standard=8
}

Attached Property

C#
/// Attached property to control the button 
/// enable or diable based on logged in user type
public class UserAcccess : FrameworkElement
{
    public static UserType GetUserAccessType(DependencyObject obj)
    {
        return (UserType)obj.GetValue(UserAccessProperty);
    }
    
    public static void SetUserAccessType(DependencyObject obj, UserType value)
    {
        obj.SetValue(UserAccessProperty, value);
    }
    
    public static readonly DependencyProperty UserAccessProperty =
        DependencyProperty.RegisterAttached("UserAccessType", 
        typeof(UserType), typeof(UserAcccess), new PropertyMetadata
        (UserType.All, new PropertyChangedCallback(UserAcccessnChanged)));
        
    private static void UserAcccessnChanged
    (DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement userAccess = (FrameworkElement)d;
        if (((UserType)e.NewValue & AppCommon.LogedUserType) == AppCommon.LogedUserType)
            userAccess.IsEnabled = true;
        else
            userAccess.IsEnabled = false;
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) cognizant
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralIts Nice Pin
Amit Gupta AG13-Feb-15 10:50
Amit Gupta AG13-Feb-15 10:50 
Questionthank you Pin
prffrost26-Nov-14 23:34
prffrost26-Nov-14 23:34 
QuestionNice article for dependency property Pin
Member 847875227-Oct-14 21:24
Member 847875227-Oct-14 21:24 
QuestionHi Baala Pin
bsreddy10069-Jul-14 19:43
bsreddy10069-Jul-14 19:43 
very good article, superb baala.
GeneralMy vote of 5 Pin
fredatcodeproject5-Dec-13 1:46
professionalfredatcodeproject5-Dec-13 1:46 
GeneralRe: My vote of 5 Pin
Balachandar Jeganathan7-Dec-13 16:59
professionalBalachandar Jeganathan7-Dec-13 16:59 
QuestionGood way. Pin
leiyangge4-Dec-13 14:35
leiyangge4-Dec-13 14:35 
AnswerRe: Good way. Pin
Balachandar Jeganathan7-Dec-13 16:59
professionalBalachandar Jeganathan7-Dec-13 16:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.