How do I make validation rules work with two controls, Part 1 of 2

Rod At Work 866 Reputation points
2020-09-14T17:57:32.193+00:00

I've got a textbox that's within a border. The idea I'm trying to do is validate that the data entered by the user. I've done this with other controls (a DatePicker control and another textbox) but in those cases it wasn't validation, it was other things which would either name the border appear or not. (I'd set the width of the border to either 1 or 0.)

In this case I really want to validate the data the user is entering into the textbox. To accomplish this, I've been following along with an example on Microsoft's Docs page "How to: Implement Binding Validation" (https://learn.microsoft.com/en-us/dotnet/desktop/wpf/data/how-to-implement-binding-validation?view=netframeworkdesktop-4.8). However, I'm not sure how to have a validation on the textbox, control the borderwidth of the border that surrounds the textbox. Here's what I've got in the XAML:

<Border Grid.Row="3"  
Grid.Column="1">  
<TextBox x:Name="MaxProficienciesTextBox"  
 Style="{StaticResource LeftAlignTextBoxStyle}"  
 Margin="0,5"  
 MaxLength="2"  
 IsEnabled="{Binding IsMaxProficienciesEnabled}"   
 KeyUp="MaxProficienciesTextBox_KeyUp">  
<TextBox.Text>  
<Binding Path="MaxProficiencies"  
 UpdateSourceTrigger="PropertyChanged">  
<Binding.ValidationRules>  
<val:MaxProficienciesValidationRule  
Min="1"  
SolutionID="{Binding SolutionID}" />  
</Binding.ValidationRules>  
</Binding>  
</TextBox.Text>  
</TextBox>  
</Border>  
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,705 questions
{count} votes