VisualStateManager.GoToState 不适用于文本框 (UWP)

VisualStateManager.GoToState not working for TextBox (UWP)

我正在尝试通过代码设置 TextBox 的 VisualState。

 <TextBox x:Name="txtbox"  Width="438" Height="56" Style="{StaticResource ExtendeTextBoxStyle}"
             PlaceholderText="{x:Bind PlaceholderText, Mode=OneWay}" ></TextBox>

代码隐藏

   private static void HasErrorUpdated(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
         EditTextControl textBox = d as EditTextControl;
        Grid sds = textBox.Content as Grid;
        var mytxt = sds.Children.FirstOrDefault() as Control;

        if (textBox != null)
        {
            if (textBox.HasError)
                VisualStateManager.GoToState(mytxt , "InvalidState", true);

            else
                VisualStateManager.GoToState(mytxt, "ValidState", false);
        }
    }

但是这种视觉状态永远不会被激活。这里有什么问题?

VisualStateManager.GoToState not working for TextBox (UWP)

请检查是否调用了GoToState,如果没有,我想你没有实现INotifyPropertyChanged接口,我查看了你之前的. I found HasError is DependencyProperty, that means that you need bind it with property has implemented PropertyChanged事件处理程序。当你调用OnPropertyChanged()方法时,它会响应propertyChangedCallback函数。

public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName]string name = "")
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
private bool _hasError;
public bool HasError
{
    get => _hasError;
    set
    {
        _hasError = value;
        OnPropertyChanged();
    }
}
private void Button_Click(object sender, RoutedEventArgs e)
{
    HasError = !HasError;
}