简体   繁体   English

如果验证失败 (UWP),则创建 VisualState

[英]Create a VisualState if validation failed (UWP)

I have Textbox inside a Usercontrol, I want to create a visualstate, if validation for a TextBox failed.我在用户控件中有文本框,如果对文本框的验证失败,我想创建一个视觉状态。 My code looks like this我的代码看起来像这样

<Grid>
    <TextBox Style="{StaticResource ExtendeTextBoxStyle}" PlaceholderText="I'am Active"   HasError="{Binding IsInvalid, UpdateSourceTrigger=PropertyChanged}"  Height="80" Width="300"  x:Name="txtActive"  Text="{Binding TextValue, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" ></TextBox>
</Grid>

Code Behind代码背后

  public EditTextControl()
    {
        this.InitializeComponent();
        this.DataContext = TV;
    }

    public bool HasError
    {
        get { return (bool)GetValue(HasErrorProperty); }
        set { SetValue(HasErrorProperty, value); }
    }

    /// <summary>
    /// This is a dependency property that will indicate if there's an error. 
    /// This DP can be bound to a property of the VM.
    /// </summary>
    public static readonly DependencyProperty HasErrorProperty =
        DependencyProperty.Register("HasError", typeof(bool), typeof(EditTextControl), new PropertyMetadata(false, HasErrorUpdated));


    // This method will update the Validation visual state which will be defined later in the Style
    private static void HasErrorUpdated(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        EditTextControl textBox = d as EditTextControl;

        if (textBox != null)
        {
            if (textBox.HasError)
                VisualStateManager.GoToState(textBox, "InvalidState", false);
            else
                VisualStateManager.GoToState(textBox, "ValidState", false);
        }
    }

But this gives me a compile-time error但这给了我一个编译时错误

Unknown member 'HasError' on element 'TextBox'  

What I am doing here, and how I can make sure that when validation for the textbox fails, my new VisualState get activated我在这里做什么,以及如何确保当文本框验证失败时,我的新VisualState被激活

TextBox has no HasError property, so we can't set it directly, we need make HasError attach property and detect TextBox TextChanged event, then check the text is valid or not. TextBox没有HasError属性,所以不能直接设置,需要让HasError附加属性并检测 TextBox 的TextChanged事件,然后检查文本是否有效。

For your requirement, we suggest use TextBoxRegex to approach.对于您的要求,我们建议使用TextBoxRegex来处理。 And use XamlBehaviors to invoke VisualState command.并使用XamlBehaviors调用VisualState命令。

For Example.例如。

<StackPanel>
    <TextBox
        Name="PhoneNumberValidator"
        extensions:TextBoxRegex.Regex="^\s*\+?\s*([0-9][\s-]*){9,}$"
        Background="Red"
        Header="Text box with Regex extension for phone number, validation occurs on TextChanged"
        >
        <Interactivity:Interaction.Behaviors>
            <Interactions:DataTriggerBehavior Binding="{Binding (extensions:TextBoxRegex.IsValid), ElementName=PhoneNumberValidator}" Value="True">
                <Interactions:GoToStateAction StateName="BlueState" />
            </Interactions:DataTriggerBehavior>
            <Interactions:DataTriggerBehavior Binding="{Binding (extensions:TextBoxRegex.IsValid), ElementName=PhoneNumberValidator}" Value="False">
                <Interactions:GoToStateAction StateName="RedState" />
            </Interactions:DataTriggerBehavior>
        </Interactivity:Interaction.Behaviors>
    </TextBox>

    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="AdaptiveVisualStateGroup">
            <VisualState x:Name="BlueState">

                <VisualState.Setters>
                    <Setter Target="PhoneNumberValidator.Background" Value="Blue" />
                </VisualState.Setters>
            </VisualState>
            <VisualState x:Name="RedState">

                <VisualState.Setters>
                    <Setter Target="PhoneNumberValidator.Background" Value="Red" />
                </VisualState.Setters>
            </VisualState>

        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
</StackPanel>

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

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