我正在尝试通过代码设置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不适用于TextBox(UWP)
我查看了您以前的case。我发现HasError
是DependencyProperty
,这意味着您需要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;
}