简体   繁体   English

如果文本框无效,则不禁用按钮

[英]Doesn't disable the button if the textbox invalid

I have the email text block with validation我有经过验证的电子邮件文本块

<TextBox x:Name="email" Style="{StaticResource emaliStyle}" Grid.Column="2" Grid.Row="2">
                <TextBox.Text>
                    <Binding  Mode="TwoWay" Path="Email"  UpdateSourceTrigger="PropertyChanged">
                        <Binding.ValidationRules>
                            <DataErrorValidationRule ValidatesOnTargetUpdated="False"/>
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>

 <Style TargetType="TextBox" x:Key="emaliStyle">
        <Setter Property="Width" Value="220"/>
        <Setter Property="Height" Value="30"/>
        <Setter Property="FontSize" Value="18"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="FontSize" Value="20"/>
        <Setter Property="Padding" Value="6,1,1,0"/>

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" 
                            BorderThickness="{TemplateBinding BorderThickness}" 
                            Background="{TemplateBinding Background}" 
                            CornerRadius="10"
                            SnapsToDevicePixels="True">
                        <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Opacity" TargetName="border" Value="0.56"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

        <Setter x:Name="LoginValidation" Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <DockPanel LastChildFill="True">
                        <TextBlock DockPanel.Dock="Bottom" Foreground="Maroon" FontSize="8pt"
                                           Text="{Binding ElementName=email, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                        </TextBlock>
                        <AdornedElementPlaceholder Name="email" />
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="True">
                <Setter Property="BorderBrush" Value="DarkRed" />
                <Setter Property="BorderThickness" Value="2" />
            </Trigger>
        </Style.Triggers>
    </Style>

the validation work and show me the message which I write in viewModel( it doesn't matter)验证工作并向我展示我在 viewModel 中编写的消息(没关系)

next step it blocks the button if the field is invalid.如果字段无效,下一步它会阻止按钮。

my button and style for it我的按钮和样式

 <Button x:Name="SignInButton" 
                    Style="{StaticResource SignInButton}"
                    Command="{Binding SignInCommand}" 
                    CommandParameter="{Binding ElementName=This}" 
                    Content="Sign In"/>

 <Style TargetType="Button" x:Key="SignInButton">
        <Setter Property="Background" Value="MidnightBlue"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="FontSize" Value="16"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="Width" Value="100"/>
        <Setter Property="Height" Value="40"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Background="{TemplateBinding Background}" CornerRadius="10">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Height="20" Margin="26,10"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="IsEnabled" Value="False"/>
        <Style.Triggers >
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding Path=(Validation.HasError), ElementName=email}" Value="False"/>
                </MultiDataTrigger.Conditions>
                <Setter Property="IsEnabled" Value="True"/>
            </MultiDataTrigger>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="LimeGreen"/>
            </Trigger>

        </Style.Triggers>
    </Style>

I write the Multitrigger (later add other textbox fields) to disable my button, but it isn't work.我编写了Multitrigger (稍后添加其他文本框字段)来禁用我的按钮,但它不起作用。

I try write all with element name Email doesn't help.我尝试用元素名称写所有Email没有帮助。 What i miss?我想念什么? because the error message will show and the field border mark red, bu isn't disable因为会显示错误信息并且字段边框标记为红色,所以 bu 未禁用

Your BlockButton() method should return false whenever you want to disable the Button that is bound to the SignInCommand .每当您想禁用绑定到SignInCommandButton ,您的BlockButton()方法都应返回false There is no reason to set the IsEnabled property in a style when you bind to a command.绑定到命令时,没有理由在样式中设置IsEnabled属性。

For the status of the command and the Button to get refeshed, you should raise the CanExecuteChanged event for the command whenever the validation status changes.要刷新命令和Button的状态,您应该在验证状态更改时引发命令的CanExecuteChanged事件。 Most ICommand implementations include a RaiseCanExecuteChanged() method or similar that lets you do this:大多数ICommand实现都包含RaiseCanExecuteChanged()方法或类似方法,可让您执行此操作:

private bool _isEmailValid;

private bool BlockButton()
{
    return _isEmailValid && IsAppDeveloper == false && IsEndUser == false;
}

public string this[string columnName]
{
    get
    {
        string error = String.Empty;
        switch (columnName)
        {
            case "Email":
                string s = ValidateModelProperty(Email, columnName);
                _isEmailValid = string.IsNullOrEmpty(s);
                SignInCommand.RaiseCanExecuteChanged();
                return s;
        }

        return error;
    }
}

private string ValidateModelProperty(object value, string propertyName)
{
    ICollection<ValidationResult> validationResults = new List<ValidationResult>();
    ValidationContext validationContext = new ValidationContext(this, null, null) { MemberName = propertyName };
    if (!Validator.TryValidateProperty(value, validationContext, validationResults))
        foreach (ValidationResult validationResult in validationResults)
            return validationResult.ErrorMessage;
    return null;
}

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

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