CommunityToolkit / CommunityToolkit/dotnet

[Question] Red border always visible

Open
#49 4 comments 0 reactions 0 assignees View on GitHub
mvvm-toolkit :toolbox: needs triage :mag:
Dominant language
C#
Stars
3.8k
Forks
400
PR merge metrics
No merged PRs in 30d

Description

Hello, I have a problem with validations. The validation border is still present after performing the validation of the property. I work with WPF

I create a course list (with 2 properties: start and end). For each race I pass the course list as a parameter.

Course class :

```c#
public class Course : ObservableValidator
{
#region Fields

private IEnumerable _courses;

private int _start;

private int _end;

#endregion

#region Properties

[CustomValidation(typeof(Course), nameof(ValidateStart))]
public int Start
{
get { return _start; }
set
{
SetProperty(ref this._start, value, true);
// ValidateProperty(this.End, nameof(End));
ValidateCourses();
}
}

[CustomValidation(typeof(Course), nameof(ValidateEnd))]
public int End
{
get { return _end; }
set
{
SetProperty(ref this._end, value, true);
// ValidateProperty(this.Start, nameof(Start));
ValidateCourses();
}
}

#endregion

#region Constructors

public Course(IEnumerable courses)
{
_courses = courses;
ValidateCourses();
}

#endregion

#region Methods

private void ValidateCourses()
{
foreach (var course in _courses)
course.ValidateAllProperties();
}

#endregion

#region Validations

public static ValidationResult ValidateStart(int start, ValidationContext context)
{
Course course = (Course)context.ObjectInstance;

List courses = course._courses.ToList();

List results = new List();

int index = courses.IndexOf(course);

if (index == 0 && start != 0)
results.Add("Start must be equal to zero");

if (start >= course.End)
results.Add("Start must be less end");

if (index != -1 && index - 1 != -1)
if (start != courses[index - 1].End)
results.Add("Start must be equal previous end");

if (results.Count == 0)
{
return ValidationResult.Success;
}

return new ValidationResult(string.Join("\n", results), new List() { nameof(Course.Start) });
}

public static ValidationResult ValidateEnd(int end, ValidationContext context)
{
Course course = (Course)context.ObjectInstance;

List courses = course._courses.ToList();

List results = new List();

int index = courses.IndexOf(course);

if (course.Start >= end)
results.Add("End must be greater Start");

if (index + 1 <= courses.Count - 1)
if (end != courses[index + 1].Start)
results.Add("End must be equal next Start");

if (results.Count == 0)
{
return ValidationResult.Success;
}

return new ValidationResult(string.Join("\n", results), new List() { nameof(Course.End) });
}

#endregion
}
```

MainViewModel class :

```c#
public class MainViewModel : ObservableObject
{
#region Fields

private ObservableCollection _courses = new ObservableCollection();

#endregion

#region Properties

public ObservableCollection Courses
{
get { return _courses; }
set { _courses = value; }
}

#endregion

#region Constructors

public MainViewModel()
{
CreateCourses();
}

#endregion

#region Methods

private void CreateCourses()
{
Courses.Add(new Course(Courses)
{
Start = 0,
End = 10
});
Courses.Add(new Course(Courses)
{
Start = 11,
End = 20
});
Courses.Add(new Course(Courses)
{
Start = 21,
End = 30
});
Courses.Add(new Course(Courses)
{
Start = 31,
End = 30
});
}

#endregion
}
```

MainWindow class

```xaml



<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>










<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding (Validation.Errors)[0].ErrorContent, RelativeSource={RelativeSource Self}}"/>
</Trigger>
</Style.Triggers>












<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<AdornedElementPlaceholder/>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ToolTip" Value="{Binding (Validation.Errors)[0].ErrorContent, RelativeSource={RelativeSource Self}}"/>
</Trigger>
</Style.Triggers>








```

When I edit a property, I check all the properties in the course list.

My problem is that the border still appears even though the property returned ValidationResult.Success

we can see that in the image below

![image](https://user-images.githubusercontent.com/25281140/144249142-e670aaca-20c6-4c16-82aa-a129e48c9c36.png)

I attach the test project.
[ProjetTest.zip](https://github.com/CommunityToolkit/WindowsCommunityToolkit/files/7634445/ProjetTest.zip)

Please can you help me

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.