繁体   English   中英

VisualStateManager.GoToState 不适用于 TextBox (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);
        }
    }

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

VisualStateManager.GoToState 不适用于 TextBox (UWP)

请检查是否调用了GoToState ,如果没有,我想你还没有实现INotifyPropertyChanged接口,我查看了你的上一个问题 我发现HasErrorDependencyProperty ,这意味着您需要将它与已实现PropertyChanged事件处理程序的属性绑定。 当你调用OnPropertyChanged()方法时,它会响应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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM