|
namespace RIADataServicesDemoApp.Controls
{
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using RIADataServicesDemoApp.Web;
/// <summary>
/// Enhances <see cref="DataForm" /> functionality by using a <see cref="PasswordBox" />
/// control for password fields and exposing a <see cref="CustomDataForm.Fields"/> collection
/// to allow runtime access to <see cref="DataForm" /> fields.
/// </summary>
public class CustomDataForm : DataForm
{
private Dictionary<string, DataField> fields = new Dictionary<string, DataField>();
/// <summary>
/// Returns a <see cref="Dictionary" /> containing all the <see cref="DataForm" />
/// <see cref="DataField" /> controls being displayed keyed by the
/// property name to which each field is bound
/// </summary>
public Dictionary<string, DataField> Fields
{
get { return this.fields; }
}
/// <summary>
/// Extends <see cref="DataForm.OnAutoGeneratingField" /> by replacing <see cref="TextBox"/>es with <see cref="PasswordBox"/>es
/// whenever applicable
/// </summary>
protected override void OnAutoGeneratingField(DataFormAutoGeneratingFieldEventArgs e)
{
// Get metadata about the property being defined
PropertyInfo propertyInfo = this.CurrentItem.GetType().GetProperty(e.PropertyName);
// Do the password field replacement if that is the case
if (e.Field.Content is TextBox && this.IsPasswordProperty(propertyInfo))
{
e.Field.ReplaceTextBox(new PasswordBox(), PasswordBox.PasswordProperty);
}
// Keep this newly generated field accessible through the Fields property
this.fields[e.PropertyName] = e.Field;
// Call base implementation (which will call other event listeners)
base.OnAutoGeneratingField(e);
}
/// <param name="propertyInfo">The entity property being analyzed</param>
/// <summary>
/// Returns whether the given property should be represented by a <see cref="PasswordBox" /> or not.
/// The default implementation will simply use a naming convention and returns true if the
/// property contains the word "Password"
/// </summary>
protected virtual bool IsPasswordProperty(PropertyInfo propertyInfo)
{
// Suggestion: to handle more complex scenarios, allow an entity to override
// this mechanism by using the System.ComponentModel.DataAnnotations.UIHintAttribute
return propertyInfo.Name.IndexOf("Password", StringComparison.InvariantCultureIgnoreCase) != -1;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)
- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence
Both of these at Sussex University UK.
Award(s)
I am lucky enough to have won a few awards for Zany Crazy code articles over the years
- Microsoft C# MVP 2016
- Codeproject MVP 2016
- Microsoft C# MVP 2015
- Codeproject MVP 2015
- Microsoft C# MVP 2014
- Codeproject MVP 2014
- Microsoft C# MVP 2013
- Codeproject MVP 2013
- Microsoft C# MVP 2012
- Codeproject MVP 2012
- Microsoft C# MVP 2011
- Codeproject MVP 2011
- Microsoft C# MVP 2010
- Codeproject MVP 2010
- Microsoft C# MVP 2009
- Codeproject MVP 2009
- Microsoft C# MVP 2008
- Codeproject MVP 2008
- And numerous codeproject awards which you can see over at my blog