我有以下声明:
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 中设置它。它说该属性不存在。我的xml是:
<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" />
设置掩码的代码完美运行。我想我也复制了所有需要的东西。为什么我不能添加另一个属性令人困惑。
如果重要的话,这是我编写的这段代码的变体:如何定义文本框输入限制?
编辑:
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));