Click here to Skip to main content
12,454,104 members (55,867 online)
Click here to Skip to main content
Add your own
alternative version

Stats

42.8K views
1.1K downloads
24 bookmarked
Posted

WPF Custom Attached Property

, 4 Dec 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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

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

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

Attached Property

/// 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)

Share

About the Author

Balachandar Jeganathan
Software Developer (Senior) cognizant
United States United States
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralIts Nice Pin
Amit Gupta AG13-Feb-15 10:50
memberAmit Gupta AG13-Feb-15 10:50 
Questionthank you Pin
prffrost26-Nov-14 23:34
memberprffrost26-Nov-14 23:34 
QuestionNice article for dependency property Pin
Member 847875227-Oct-14 21:24
memberMember 847875227-Oct-14 21:24 
QuestionHi Baala Pin
bsreddy10069-Jul-14 19:43
memberbsreddy10069-Jul-14 19:43 
GeneralMy vote of 5 Pin
fredatcodeproject5-Dec-13 1:46
memberfredatcodeproject5-Dec-13 1:46 
GeneralRe: My vote of 5 Pin
Bala Chandar7-Dec-13 16:59
memberBala Chandar7-Dec-13 16:59 
QuestionGood way. Pin
leiyangge4-Dec-13 14:35
memberleiyangge4-Dec-13 14:35 
AnswerRe: Good way. Pin
Bala Chandar7-Dec-13 16:59
memberBala Chandar7-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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160826.1 | Last Updated 4 Dec 2013
Article Copyright 2013 by Balachandar Jeganathan
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid