How do I set default values for my custom wpf components? I have a Textfield with the property "public Protection Protection{set;get;}". Protection is an enum:
public class Field3270Attributes{
public enum Protection
{
PROTECTED,
UNPROTECTED,
AUTOSKIP
}
}
The default value should be autoskip but protected is listed as the default value in the wpf designer since its the first element in the enum. Setting the protection in the Textfield constructor did not help. I've tried DependencyProperty which does work but I have to specify a callback(setProtection) if I want any values except the default value to work. If I dont specify a callback, changing values inside the wpf designer has no effect. Is there a way to get the same behavior without开发者_Python百科 having to specify callback methods for every property?
public class Textfield{
public static readonly DependencyProperty ProtectionProperty =
DependencyProperty.Register("Protection",
typeof(Field3270Attributes.Protection),
typeof(Textfield3270),
new FrameworkPropertyMetadata(Field3270Attributes.Protection.PROTECTED, setProtection));
private static void setProtection(object sender, DependencyPropertyChangedEventArgs e)
{
Textfield field = (Textfield)sender;
field.Protection = (Field3270Attributes.Protection)e.NewValue;
}
private Field3270Attributes.Protection protection;
public Field3270Attributes.Protection Protection
{
get
{
return protection;
}
set
{
this.protection = value;
if (value == Field3270Attributes.Protection.UNPROTECTED)
{
this.IsReadOnly = false;
Background = Brushes.White;
}
else
{
this.IsReadOnly = true;
Background = Brushes.LightSteelBlue;
}
}
}
public Textfield3270()
{
this.Protection = Field3270Attributes.Protection.PROTECTED;
}
}
Your DependencyProperty
definition defines the default value. Change the first parameter in the FrameworkPropertyMetadata
from PROTECTED
to AUTOSKIP
public static readonly DependencyProperty ProtectionProperty =
DependencyProperty.Register("Protection",
typeof(Field3270Attributes.Protection),
typeof(Textfield3270),
new FrameworkPropertyMetadata(Field3270Attributes.Protection.AUTOSKIP, setProtection));
EDIT
You are overwriting your Protection
DependencyProperty
by implementing your own version with the same name. Remove your definition of the Protection {get; set;}
completely.
If you want them to show up in the XAML designer, define the Get/Set as static methods like so:
public static Field3270Attributes.Protection GetProtection(DependencyObject obj)
{
return (Field3270Attributes.Protection)obj.GetValue(ProtectionProperty);
}
public static void SetProtection(DependencyObject obj, Field3270Attributes.Protection value)
{
obj.SetValue(ProtectionProperty, value);
}
If you want to attach some logic on PropertyChanged, you can use this code in your class constructor:
DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(Textfield.ProtectionProperty, typeof(Textfield));
if (dpd != null) dpd.AddValueChanged(this, delegate { Protection_Changed(); });
And your changed method would look like this:
private void Protection_Changed()
{
Field3270Attributes.Protection protection = GetProtection(this);
// Do something with value
}
精彩评论