English 中文(简体)
依赖性属性 - 无法从 XAML 设定值
原标题:Dependency Property - Unable to Set Value From 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); }
    }

目前这个代码没有编译, 是因为我还没有添加 : 依附性属性 。 当我添加该代码时, 它表示字符串 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" />

设置面具的代码运作完美。 我想我也复制了所有必需的东西。 令人困惑的是, 为什么我不能添加其它属性 。

如果重要的话,这就是我所写的这个代码的变式: 如何定义TextBox输入限制?

编辑:

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));
最佳回答

您所张贴的代码显示您正在注册一个 < a href=" http://msdn. microsoft.com/ en- us/library/ ms749011" rel = "nofollow" >AttachedProperty 因此 PassColorProperty 不属于您的色彩映射类的附属属性。 它必须通过附着该属性的天体访问 。 所附属性允许您将该属性设置在其他对象上, 而不仅仅是其它对象上 。

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

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

除了”外, http://msdn.microsoft.com/en-us/library/ms749011" rel=“nofollow”>MSDN 解释附着财产的存取者:

取用存取器

GetProperty 访问器的签名必须是:

获取属性Name(目标目标)

-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.

Set 访问器

SetPropertyName 访问器的签名必须是 :

设置属性Name(目标目标,对象值)

-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).

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签