【问题标题】:VisualStateManager.GoToState not working for TextBox (UWP)VisualStateManager.GoToState 不适用于 TextBox (UWP)
【发布时间】:2020-08-17 14:47:36
【问题描述】:

我正在尝试通过代码设置 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方法的返回值。是否返回true

标签: c# xaml uwp visualstatemanager


【解决方案1】:

VisualStateManager.GoToState 不适用于 TextBox (UWP)

请检查GoToState是否被调用,如果没有,我想你没有实现INotifyPropertyChanged接口,我查看了你之前的question。我发现HasErrorDependencyProperty,这意味着您需要将它与已实现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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 2021-07-22
    • 2017-01-15
    • 2019-11-02
    • 1970-01-01
    • 2011-07-26
    • 2022-01-08
    相关资源
    最近更新 更多