VisualStateManager.GoToState不适用于TextBox(UWP)

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

我正在尝试通过代码设置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);
        }
    }

但是这种视觉状态永远不会被激活。这是怎么了?

c# xaml uwp visualstatemanager
1个回答
0
投票

VisualStateManager.GoToState不适用于TextBox(UWP)

我查看了您以前的case。我发现HasErrorDependencyProperty,这意味着您需要bind属性已经实现了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;
}
© www.soinside.com 2019 - 2024. All rights reserved.