编程语言
首页 > 编程语言> > c#-依赖属性-无法从XAML设置值

c#-依赖属性-无法从XAML设置值

作者:互联网

我有以下声明:

public static readonly DependencyProperty PassColorProperty = DependencyProperty.RegisterAttached("PassColor",
            typeof(string),
            typeof(ColorMasking),
            new PropertyMetadata("#FFCCFF"));

    public string PassColor
    {
        get { return (string)GetValue(PassColorProperty); }
        set { SetValue(PassColorProperty, value); }
    }

目前,由于我尚未在类中添加DependencyProperty,因此该代码无法编译.当我添加该代码时,它说字符串PassColor无效.

根本没有字符串,代码可以编译,我可以从该类中设置读取属性.我无法通过XAML进行设置.它说该属性不存在.我的xaml是:

<TextBox Grid.Column="1" Grid.Row="8" Margin="3" Width="Auto" Height="Auto" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" 
                b:ColorMasking.Mask=" ... Long Regex Command ... "
                b:ColorMasking.PassColor="99FF99" />

设置遮罩的代码可以完美工作.我想我也复制了所有必需的内容.关于为什么我不能添加另一个属性,这令人困惑.

如果重要的话,这是我编写的以下代码的变体:How to define TextBox input restrictions?

编辑:

public class ColorMasking : DependencyObject
{
    private static readonly DependencyPropertyKey _maskExpressionPropertyKey = DependencyProperty.RegisterAttachedReadOnly("MaskExpression",
            typeof(Regex),
            typeof(ColorMasking),
            new FrameworkPropertyMetadata());

    /// <summary>
    /// Identifies the <see cref="Mask"/> dependency property.
    /// </summary>
    /// 
    public static readonly DependencyProperty PassColorProperty = DependencyProperty.Register("PassColor",
            typeof(string),
            typeof(ColorMasking),
            new PropertyMetadata("#99FF99"));

    public static readonly DependencyProperty FailColorProperty = DependencyProperty.Register("FailColor",
            typeof(string),
            typeof(ColorMasking),
            new PropertyMetadata("#FFCCFF"));

    public static readonly DependencyProperty MaskProperty = DependencyProperty.RegisterAttached("Mask",
            typeof(string),
            typeof(ColorMasking),
            new FrameworkPropertyMetadata(OnMaskChanged));

解决方法:

您发布的代码表明您正在注册AttachedProperty,因此PassColorProperty不是您的ColorMasking类的DependencyPropery.必须通过在其上设置了附加属性的对象来访问它.附加的属性将允许您在其他对象上设置该属性,而不仅仅是

    public static void SetPassColor(DependencyObject obj, string passColor)
    {
        obj.SetValue(PassColorProperty, passColor);
    }

    public static string GetPassColor(DependencyObject obj)
    {
        return (string)obj.GetValue(PassColorProperty);
    }

MSDN之外的其他内容解释了附加属性的访问器:

The Get Accessor

The signature for the GetPropertyName accessor must be:

public static object Get PropertyName (object target )

-The target object can be specified as a more specific type in your
implementation. For example, the DockPanel.GetDock method types the
parameter as UIElement, because the attached property is only intended
to be set on UIElement instances.

-The return value can be specified as a more specific type in your
implementation. For example, the GetDock method types it as Dock,
because the value can only be set to that enumeration.

The Set Accessor

The signature for the SetPropertyName accessor must be:

public static void Set PropertyName (object target , object value )

-The target object can be specified as a more specific type in your
implementation. For example, the SetDock method types it as UIElement,
because the attached property is only intended to be set on UIElement
instances.

-The value object can be specified as a more specific type in your
implementation. For example, the SetDock method types it as Dock,
because the value can only be set to that enumeration. Remember that
the value for this method is the input coming from the XAML loader
when it encounters your attached property in an attached property
usage in markup. That input is the value specified as a XAML attribute
value in markup. Therefore there must be type conversion, value
serializer, or markup extension support for the type you use, such
that the appropriate type can be created from the attribute value
(which is ultimately just a string).

标签:masking,dependency-properties,wpf,xaml,c
来源: https://codeday.me/bug/20191201/2079651.html