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

时间:2016-05-11 02:25:12

标签: c# wpf validation

我试图让文本框的边框呈空红色。这是我的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>

我试图设置样式:

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

EmptyRule:

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

        }
    }

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

2 个答案:

答案 0 :(得分:3)

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

DataContext是一种了解XAML(视图,您的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; }
    }
}

然后,您应该将DataContext设置为Window。例如,在代码隐藏中:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new SomeUser();       
    }
}

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

答案 1 :(得分:0)

您的BindingText应该是这样的......

<Binding Path="Text" UpdateSourceTrigger="LostFocus" Mode="OneWayToSource" NotifyOnValidationError="True" RelativeSource="{RelativeSource Self}">