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

时间:2019-06-19 10:26:11

标签: c# wpf validation xaml mvvm

我有带有验证的电子邮件文本块

<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>

验证工作并向我显示我在viewModel中编写的消息(没关系)

下一步,如果该字段无效,它将阻止按钮。

我的按钮及其样式

 <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>

我写了Multitrigger(后来添加了其他文本框字段)以禁用我的按钮,但这是行不通的。

我尝试用元素名称Email编写所有内容都无济于事。我想念什么? 因为将显示错误消息并且字段边框标记为红色,所以bu未被禁用

1 个答案:

答案 0 :(得分:1)

每当您要禁用绑定到BlockButton()的{​​{1}}时,您的false方法应返回Button。绑定到命令时,无需设置样式的SignInCommand属性。

要使命令的状态和IsEnabled重新显示,每当验证状态更改时,都应引发命令的Button事件。大多数CanExecuteChanged实现都包含一个ICommand方法或类似方法,可让您执行以下操作:

RaiseCanExecuteChanged()