2016-05-11 132 views
2

我試圖在文本框爲空時使文本框的邊框變紅。這是我的XAML:驗證WPF文本框設置紅色邊框

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

     } 
    } 

在調試器,它看起來像驗證方法是根本不使用。 我在做什麼錯?

回答

0

BindingText應該是這樣的......

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

我不能看到你的XAML和視圖模型之間設置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; } 
    } 
} 

那麼你應該設置DataContextWindow。例如,在後臺代碼:

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

@PiotrŁazarczyk隨意問任何問題。如果您覺得我的回覆對您有幫助,那麼您可以將我的回覆標記爲答案,以簡化未來搜索其他人的過程。請閱讀http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – StepUp