EditableAttribute (data modeling attribute) C# with Example



EditableAttribute (data modeling attribute) C# with Example

EditableAttribute sets whether users should be able to change the value of the class property. 
public class Employee 
{ 
[Editable(false)] 
public string FirstName { get; set; } 
} 
Simple usage example in XAML application 
 
 
 
 
 
 
 
 
 
 
namespace WpfApplication 
{ 
///  
/// Interaction logic for MainWindow.xaml 
///  
public partial class MainWindow : Window 
{ 
private Employee _employee = new Employee() { FirstName = "This is not editable"}; 
public MainWindow() 
{ 
InitializeComponent(); 
DataContext = this; 
} 
public Employee Employee 
{ 
get { return _employee; } 
set { _employee = value; } 
} 
} 
} 
namespace WpfApplication 
{ 
public class EditableConverter : IValueConverter 
 

{ 
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
{ 
// return editable attribute's value for given instance property, 
// defaults to true if not found 
var attribute = value.GetType() 
.GetProperty(parameter.ToString()) 
.GetCustomAttributes(false) 
.OfType() 
.FirstOrDefault(); 
return attribute != null ? attribute.AllowEdit : true; 
} 
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo 
culture) 
{ 
throw new NotImplementedException(); 
} 
} 
} 
 

Keywords are predefined, reserved identifiers with special meaning to the compiler. They cannot be used as 
identifiers in your program without the @ prefix. For example @if is a legal identifier but not the keyword if. 

0 Comment's

Comment Form

Submit Comment