c# - 如何在 WPF 中将绿色 IDataErrorInfo 标记为 true

标签 c# wpf xaml mvvm-light

我有 ViewModel IDataErrorInfo

我有一个带有数据注释的属性:

[Required(ErrorMessage = "Login field should be filled in")]
[RegularExpression(@"\w+(@)[a-zA-z]+(\.)[a-zA-z]+", ErrorMessage = ("Use the right email format"))]
public string Email
{
    get => authRequstModel.Email;
    set => Set(ref authRequstModel.Email, value);
}

在 XAML 中我有一个文本框:

<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="204"/>
        <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="5,2,1,0"/>
        <Setter Property="SelectionBrush" Value="Red"/>
        <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>
                        <Border BorderBrush="DarkRed" BorderThickness="2" CornerRadius="10">
                            <AdornedElementPlaceholder Name="email" />
                        </Border>
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
</Style>

如果无效,则标记为红色,但如果有效,如何标记为绿色?

最佳答案

您可以通过创建自定义 ControlTemplate 来扩展您的 Style:

<Style TargetType="TextBox" x:Key="emaliStyle">
    <Setter Property="Width" Value="204"/>
    <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="5,2,1,0"/>
    <Setter Property="SelectionBrush" Value="Red"/>
    <Setter Property="BorderThickness" Value="2" />
    <Setter Property="BorderBrush" Value="Green" />
    <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>
    <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>
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="BorderBrush" Value="DarkRed" />
        </Trigger>
    </Style.Triggers>
</Style>

关于c# - 如何在 WPF 中将绿色 IDataErrorInfo 标记为 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56633300/

相关文章:

c# - 使用 ToList() 与使用 IList 从存储库返回 IEnumerable

c# - 全局数据真的那么糟糕吗?

wpf - ResourceDictionary 源绑定(bind)到模块(用于本地化)

xaml - UWP 文本框不会在 StackPanel 中拉伸(stretch)

wpf 在绑定(bind)项上捕获字符串格式 N2 的文本框的无效输入

c# - 以编程方式在新形状文件中设置属性表

c# - 明确的本地范围——有什么真正的好处吗?

c# - 如何在 Razor 编辑 View 中显示选中的单选按钮 Asp net core mvc

c# - 我如何测试 Relaycommand?

wpf - 当 ListView 的 SelectedItem 从 null 更改时启用按钮并执行相反的操作