绑定样式给出错误

时间:2014-03-27 06:45:19

标签: wpf

我已编写代码来附加属性Mask.But我收到错误

无法解析Style Property' Marsk'。验证拥有类型是Style的TargetType,或使用Class.Property语法指定属性。)

public static MaskType GetMask(DependencyObject obj)
{
    return (MaskType)obj.GetValue(MaskProperty);
}

public static void SetMask(DependencyObject obj, MaskType value)
{
    obj.SetValue(MaskProperty, value);
}

public static readonly DependencyProperty MaskProperty =
    DependencyProperty.RegisterAttached(
        "Mask",
        typeof(MaskType),
        typeof(pMaskableTextBox),
        new FrameworkPropertyMetadata(MaskChangedCallback)
        );

private static void MaskChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    if (e.OldValue is TextBox)
    {
        (e.OldValue as TextBox).PreviewTextInput -= TextBox_PreviewTextInput;
        DataObject.RemovePastingHandler((e.OldValue as TextBox), (DataObjectPastingEventHandler)TextBoxPastingEventHandler);
    }

    TextBox _this = (d as TextBox);
    if (_this == null)
        return;

    if ((MaskType)e.NewValue != MaskType.Any)
    {
        _this.PreviewTextInput += TextBox_PreviewTextInput;
        DataObject.AddPastingHandler(_this, (DataObjectPastingEventHandler)TextBoxPastingEventHandler);
    }

    ValidateTextBox(_this);
}

我在样式rStyles.XAML中调用了这个属性

xmlns:pBasePage="clr-namespace:Parts.Pages.Page;assembly=Parts.Pages.Page"

<Style x:Key="styDecimalTextBox" TargetType="{x:Type TextBox}">
    <Setter Property="pBasePage:Parts.Pages.Page.pMaskableTextBox.Mask" Value ="Decimal"  />
</Style>

1 个答案:

答案 0 :(得分:0)

错误说明了一切:

  

无法解析样式属性'Mask'。验证拥有类型   是Style的TargetType,或使用Class.Property语法来指定   属性。)

检查TargetType:

<Style x:Key="styDecimalTextBox" TargetType="{x:Type YourTextBoxWithTheMaskProperty}">
    <Setter Property="pBasePage:Parts.Pages.Page.pMaskableTextBox.Mask" Value ="Decimal"  />
</Style>