WPF TextBox 在验证上设置红色边框

Pio*_*r L 7 c# validation wpf

我试图在文本框为空时将其边框设为红色。这是我的 xaml:

     <TextBox  Style="{StaticResource TextBoxEmptyError}" Name="tbFilename" Grid.Column="1" >
         <Binding Path="Text" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True">
            <Binding.ValidationRules>
                <local:EmptyRule />
            </Binding.ValidationRules>
        </Binding>
    </TextBox>
Run Code Online (Sandbox Code Playgroud)

我正在尝试设置的样式:

        <Style x:Key="TextBoxEmptyError" TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="True">
                    <Setter Property="BorderThickness" Value="2" />
                    <Setter Property="BorderBrush" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
Run Code Online (Sandbox Code Playgroud)

空规则:

public class EmptyRule : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (string.IsNullOrEmpty(value as string))
                return new ValidationResult(false, null);
            else
                return new ValidationResult(true, null);

        }
    }
Run Code Online (Sandbox Code Playgroud)

在调试器中,看起来根本没有使用 Validation 方法。我究竟做错了什么?

Ste*_*pUp 7

我看不到您DataContext在 XAML 和 viewModel 之间设置的位置。

DataContext是一种了解 XAML(View, your Window) 可以从何处获取数据的方法。

例如,您有模型类:

internal class SomeUser
{        
    private string _name;
    private string _address;

    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
        }
    }

    public string Address
    {
        get { return _address; }
        set { _address = value; }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你应该设置DataContext为你的Window. 例如,在代码隐藏中:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new SomeUser();       
    }
}
Run Code Online (Sandbox Code Playgroud)

那么 XAML 应该是这样的:

<Grid>
  <Grid.Resources> 
     <Style x:Key="CustomTextBoxTextStyle" TargetType="TextBox">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Border x:Name="bg" BorderBrush="#FF825E5E" BorderThickness="1">
                            <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                        <ControlTemplate.Triggers>

                            <Trigger Property="Validation.HasError" Value="True">
                                <Trigger.Setters>
                                    <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},Path=(Validation.Errors)[0].ErrorContent}"/>                                        
                                    <Setter Property="BorderThickness" TargetName="bg" Value="2"/>
                                    <Setter Property="BorderBrush" TargetName="bg" Value="Red"/>
                                </Trigger.Setters>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Resources>
</Grid>

<TextBox Style="{StaticResource CustomTextBoxTextStyle}" Height="23" Name="textBox1" Margin="25">
            <Validation.ErrorTemplate>
                <ControlTemplate>
                    <DockPanel>
                        <TextBlock Foreground="Red" DockPanel.Dock="Right">!</TextBlock>
                        <AdornedElementPlaceholder x:Name="ErrorAdorner"
    ></AdornedElementPlaceholder>
                    </DockPanel>
                </ControlTemplate>
            </Validation.ErrorTemplate>

            <TextBox.Text>
                <Binding Path="Name" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>
                        <local:NameValidator></local:NameValidator>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
</TextBox>
Run Code Online (Sandbox Code Playgroud)