Autor Beitrag
RobAll
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 34



BeitragVerfasst: Do 08.04.21 11:38 
Hallo Zusammen,

Im XAML nutze ich zur Validierung einer Adresse
ausblenden XML-Daten
1:
2:
3:
<Binding.ValidationRules>
  <vr:AdressValidationRule />
</Binding.ValidationRules>

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
//Validierungsklasse
public class AdressValidationRule : ValidationRule
{
  //...
}

Diese Validierung möchte ich nun im ViewModel "CanExecute" eines CommandBinding nutzen.

Die Lösung über XAML Has Error will ich nicht verwenden
ausblenden XML-Daten
1:
<DataTrigger Binding="{Binding ElementName=Adresse, Path=(Validation.HasError)}"					


Kann ich den Error aus dem XAML ins ViewModel binden und für das CanExecute verwenden?

Welche Lösung verwendet ihr?

Danke vorab
Rob ALL

Moderiert von user profile iconTh69: XML-Tags hinzugefügt
Moderiert von user profile iconTh69: C#-Tags hinzugefügt
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Do 08.04.21 15:40 
Du kannst doch einfach ein Objekt deiner ValidationRule-Klasse erstellen und dessen Validate-Methode aufrufen.
RobAll Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 34



BeitragVerfasst: Fr 09.04.21 16:21 
Th69 erstmal danke..

Dein Vorschlag funktioniert fast..

Die Validation Rule verhindert einen korrupten Wert in der gebundenen Eigenschaftsmethode, erstmal gut, aber die "e.CanExecute = true" bleibt true.

- Wie kann ich explizit einen korrupten Wert über XAML ins ViewModel Feld binden, um mit dem Command zu arbeiten?
Irgendwie widerspricht sich das? Gibt es noch eine andere Möglichkeit zu meiner ursprünglichen Frage?

- Die ValidationRule erwartet als zweites Argument CultureInfo hier übergebe ich null -> Für was wird diese benötigt?
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
private void OpenCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    if (AdressValidationRule.Validate(Adress, null).IsValid)
    {
        e.CanExecute = true;
    }
    else
    {
        e.CanExecute = false;
    }
}


Moderiert von user profile iconTh69: C#-Tags hinzugefügt
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Fr 09.04.21 17:18 
Dort solltest du CultureInfo.CurrentCulture verwenden.

Deine Frage verstehe ich jedoch nicht!?
RobAll Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 34



BeitragVerfasst: Fr 09.04.21 18:19 
Hallo Th69

Die Validation Rule lässt nur einen gültigen Wert im Setter
zu:
ausblenden C#-Quelltext
1:
2:
3:
set {
    SetProperty(ref _Adress, value);                
}

ungültige Werte werden nicht ins Modell geupdatet. Erstmal gut, aber

wie kann e.CanExecute des Commands reagieren, wenn die Eigenschaft "Adress" nur gültige Werte annimmt?

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
private void OpenCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    if (AdressValidationRule.Validate(Adress, null).IsValid)
    {
        e.CanExecute = true;
    }
    else
    {
        e.CanExecute = false;
    }
}

Moderiert von user profile iconTh69: C#-Tags hinzugefügt
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Sa 10.04.21 09:51 
Ok, jetzt verstehe ich (hoffentlich). Vllt. hilft dir MVVM - Command und ValidationRule - Problem?

Ansonsten gibt es ja noch andere Validierungsmöglichkeiten, s. z.B. Data Validation in WPF.
How to call CanExecute when validation cancels UpdateSourceTrigger verwendet IDataErrorInfo.

PS: Bitte verwende selber die passenden C#- oder XML-Tags in deinen Beiträgen.