VisualStateManager.GoToState 不适用于 TextBox (UWP) [英] VisualStateManager.GoToState not working for TextBox (UWP)

查看:20
本文介绍了VisualStateManager.GoToState 不适用于 TextBox (UWP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to set VisualState of a TextBox through code.

 <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);
        }
    }

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

But this visual state never gets activated. What is wrong here?

推荐答案

VisualStateManager.GoToState 不适用于 TextBox (UWP)

VisualStateManager.GoToState not working for TextBox (UWP)

请检查GoToState是否被调用,如果没有,我想你没有实现INotifyPropertyChanged接口,我查看了你之前的问题.我发现 HasErrorDependencyProperty,这意味着您需要将其与已实现的属性绑定PropertyChanged 事件处理程序.当您调用 OnPropertyChanged() 方法时,它会响应 propertyChangedCallback 函数.

Please check if GoToState has been called, if not, I suppose you have not implemented INotifyPropertyChanged interface, I viewed your previous question. I found HasError is DependencyProperty, that means that you need bind it with property has implemented PropertyChanged event handler. When you call OnPropertyChanged() method, it will response propertyChangedCallback function.

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;
}

这篇关于VisualStateManager.GoToState 不适用于 TextBox (UWP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆