gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:24:34 26 4
gpt4 key购买 nike

我有以下声明:

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

关于c# - 依赖属性 - 无法从 XAML 设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10757666/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com