TextBox的DependencyProperty给出编译时错误(UWP)

问题描述 投票:0回答:1

我有一个带有DependencyProperty的文本框,代码看起来像这样

<UserControl
x:Class="Projectname.Controls.Editors.EditTextControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:ui="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
mc:Ignorable="d">

<Grid>
    <TextBox  PlaceholderText="I'am Active"   HasError="{Binding IsInvalid, UpdateSourceTrigger=PropertyChanged}"  Height="80" Width="300"  x:Name="txtActive"  Text="{Binding TextValue, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" ></TextBox>
</Grid>

  public sealed partial class EditTextControl : UserControl
{
    TestViewModel TV = new TestViewModel();
    public EditTextControl()
    {
        this.InitializeComponent();
        this.DataContext = TV;
    }

    public bool HasError
    {
        get { return (bool)GetValue(HasErrorProperty); }
        set { SetValue(HasErrorProperty, value); }
    }

    /// <summary>
    /// This is a dependency property that will indicate if there's an error. 
    /// This DP can be bound to a property of the VM.
    /// </summary>
    public static readonly DependencyProperty HasErrorProperty =
        DependencyProperty.Register("HasError", typeof(bool), typeof(EditTextControl), new PropertyMetadata(false, HasErrorUpdated));


    // This method will update the Validation visual state which will be defined later in the Style
    private static void HasErrorUpdated(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        EditTextControl textBox = d as EditTextControl;

        if (textBox != null)
        {
            if (textBox.HasError)
                VisualStateManager.GoToState(textBox, "InvalidState", false);
            else
                VisualStateManager.GoToState(textBox, "ValidState", false);
        }
    }
}

对我来说,一切看起来不错,但是在编译时本身,却出现了这些错误。

The property 'HasError' was not found in type 'TextBox'.
The member "HasError" is not recognized or is not accessible.

有人可以指出我在这里做错了吗?

c# xaml uwp dependency-properties
1个回答
1
投票

HasErrorEditTextControl用户控件上的属性,而不是TextBox上的属性。如果要向TextBox类添加自定义属性,请使用Attached Propert y而不是Dependency属性。

© www.soinside.com 2019 - 2024. All rights reserved.