Click here to Skip to main content
Click here to Skip to main content

WPF Custom Attached Property

, 4 Dec 2013
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

Bala Chandar

India India
No Biography provided

Comments and Discussions

 
QuestionHi Baala Pinmemberbsreddy10069-Jul-14 19:43 
GeneralMy vote of 5 Pinmemberfredatcodeproject5-Dec-13 1:46 
GeneralRe: My vote of 5 PinmemberBala Chandar7-Dec-13 16:59 
QuestionGood way. Pinmemberleiyangge4-Dec-13 14:35 
AnswerRe: Good way. PinmemberBala Chandar7-Dec-13 16:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.8.140821.2 | Last Updated 4 Dec 2013
Article Copyright 2013 by Bala Chandar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid