DependencyProperty для TextBox выдает ошибку времени компиляции (UWP) - PullRequest
0 голосов
/ 02 мая 2020

У меня есть текстовое поле с 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.

Может кто-нибудь указать, что я здесь не так делаю?

1 Ответ

1 голос
/ 02 мая 2020

HasError - это свойство пользовательского элемента управления EditTextControl , а не * TextBox . Если вы хотите добавить пользовательское свойство в класс TextBox, вы используете Attached Propert y, а не свойство Dependency.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...