2

I have a View which contains the userprofile of the current user. The view contains a viewmodel with a two-way binding to the single attributes like username or email adress. The view also has a behaviour, which validates the input and shows an error, of the input is not valid. Because of the two-way binding, the viewmodel updates the value even if the behavior says the input is wrong. I need to solve that.

My current approach is to use include the behavior in the viewmodel as a attribute. So I can access the attributes of the behavior in the setter of the attributes in the viewmodel. So I cant stop the update to the wrong inputs. But I can not get access the behavior from my viewmodel in the xaml of the view. Is that a way I can do it at all?

My next approach would be to pass the "isValid" attribute of the behavior to the viewmodel. But here again, I don't know how to do it, is it possible at all?

Last approach would be to create a command in the viewmodel, binding it to a new button in the userprofile, using a one-way binding and somehow pass the inputs from the view to the command and updateing the userprofile.

May be here is someone who can help me?

EDIT: I post some code:

This is a single entry with my behavior:

<Entry x:Name="phoneNumber" Text="{Binding TelephoneNum, Mode=TwoWay}">
    <Entry.Behaviors>
        <behaviors:TelNumBehavior x:Name="NumValidatorUser"/>
    </Entry.Behaviors>
</Entry>

The TelNumBehavior:

public class TelNumBehavior : Behavior<Entry>
{
...
public static readonly BindablePropertyKey IsVisiblePropertyKey = 
        BindableProperty.CreateReadOnly("IsVisible", typeof(bool), typeof(TelNumBehavior), false);
public static readonly BindableProperty IsVisibleProperty = IsVisiblePropertyKey.BindableProperty;

...
public bool IsVisible
{
    get { return (bool) this.GetValue(IsVisibleProperty); }
    set
      {
          this.SetValue(IsVisiblePropertyKey, value);
       }
    }
...
(OnAttachedTo, OnDetachingFrom)
...

private void bindable_TextChanged(object sender, TextChangedEventArgs e)
{
    Entry entry = sender as Entry;
    this.IsVisible = (entry.Text == "")
            ? true
            : (Regex.IsMatch(e.NewTextValue, mobileRegex) || Regex.IsMatch(e.NewTextValue, fixedLineRegex));
    entry.TextColor = this.IsVisible ? Color.Default : Color.Red;
}

To sum it up: I use that behavior for validation of some inputs. But the viewmodel does not know if the inputs are correct or not. And this is currently my problem. I dont know how to inform the viewmodel about the state of the inputs.

}
chris000r
  • 297
  • 3
  • 10
  • I am not sure if you implemented data validation or not. if not, look here: https://learn.microsoft.com/en-us/dotnet/framework/wpf/data/data-binding-overview?view=netframework-4.7.2#data-validation if you use the built-in validation please post some xaml code and also the code of your behavior – Jan Sep 29 '18 at 20:16
  • See my edit @Jan – chris000r Sep 30 '18 at 10:42
  • I had the same issue here is my own solution https://stackoverflow.com/questions/52392311/how-to-get-invalid-values-in-a-wpf-view – Jan Sep 30 '18 at 16:10
  • as for your code: Data Validation is what you are looking for. don't write your own behaviours to mimic functions WPF has solved way more elegantly. if a validation in wpf fails the view model's setter will never be reached. to check this. check the wpf samples and here the project data binding/validateitemsample. you will see that if your data fails validation, the value in the VM is untouched, no need for extra checks in the setter as you had it in your other question of the same issue. https://github.com/Microsoft/WPF-Samples – Jan Oct 01 '18 at 04:05

1 Answers1

1

I think, it's a little late to answer it but I had the same thing today.

How I solved.

  1. In your cs side, where you get instace of the viewmodel ( you should make it singleton)

  2. In your TelNumBehavior class, you should use that singleton instance.

  3. From that instance, you can trigger a command for each time the entry changes.

If you still have same issue, I can share the code too.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Fethullah Kaya
  • 194
  • 4
  • 16