开发者

c# - How to set default values of custom controls for wpf designer?

开发者 https://www.devze.com 2023-03-28 23:33 出处:网络
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:

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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号