简体   繁体   English

c# - 如何为 wpf 设计器设置自定义控件的默认值?

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

How do I set default values for my custom wpf components?如何为我的自定义 wpf 组件设置默认值? I have a Textfield with the property "public Protection Protection{set;get;}".我有一个属性为“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.默认值应该是 autoskip 但 protected 在 wpf 设计器中列为默认值,因为它是枚举中的第一个元素。 Setting the protection in the Textfield constructor did not help.在 Textfield 构造函数中设置保护没有帮助。 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.我试过 DependencyProperty 确实有效,但如果我想要除默认值以外的任何值都可以工作,我必须指定一个回调(setProtection)。 If I dont specify a callback, changing values inside the wpf designer has no effect.如果我不指定回调,则在 wpf 设计器中更改值无效。 Is there a way to get the same behavior without 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.您的DependencyProperty定义定义了默认值。 Change the first parameter in the FrameworkPropertyMetadata from PROTECTED to AUTOSKIPFrameworkPropertyMetadata中的第一个参数从PROTECTED更改为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.您正在通过实现您自己的同名版本来覆盖您的Protection DependencyProperty Remove your definition of the Protection {get; set;}删除您对Protection {get; set;} Protection {get; set;} completely. Protection {get; set;}完全。

If you want them to show up in the XAML designer, define the Get/Set as static methods like so:如果您希望它们出现在 XAML 设计器中,请将 Get/Set 定义为 static 方法,如下所示:

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:如果要在 PropertyChanged 上附加一些逻辑,可以在 class 构造函数中使用此代码:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM