2016-05-11 120 views
2

空のときにテキストボックスの枠線を赤くしたい。ここに私のXAMLのです:WPF TextBoxで赤い枠線の設定が有効になった

 <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

あなたTextからBindingは次のようにする必要があります...

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

あなたは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; } 
    } 
} 

次に、あなたがあなたのWindowDataContextを設定する必要があります。例えば、コードビハインドで:PiotrŁ[email protected]

<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

はどんな質問をすること自由に感じ:

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

その後、XAMLは、このようになります必要があります。私の返事があなたに役立つと感じたら、私の返事を答えとしてマークして、将来の他の人の検索を簡単にすることができます。 http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-workをご覧ください。 – StepUp

関連する問題