0

Maybe this is easy, but I didn't find a solution for my problem yet.

When I use binding and a user changes for example the text in a textbox, how can I perform some backround checks like:

  • Is this name already in my database
  • Does the name fit to my allowed character set

Without binding this is easy i just call the functions that do the trick.

Example:

<TextBox x:Name="textbox_Name" Height="23" Margin="108,37,20,0" TextWrapping="Wrap" Text="{Binding Name,UpdateSourceTrigger=LostFocus}" VerticalAlignment="Top"/>
  • The datacontext is ObjectXYZ.
  • ObjectXYZ has a Name and a Description property.
  • I also have a Database with a ObservableCollection of type "ObjectXYZ" called "list"

Normaly I woud do something like: if(!Database.isExistingObject(textbox_Name.Text) { ObjectXYZ.Name=textbox_Name.Text; }

With binding the name gets directly changed(Two way binding)...how can I check it before its changed?

2
  • Without binding this is easy i just call the functions that do the trick. With the binding, you can still call the same functions.
    – Bolu
    Jan 9, 2015 at 9:22
  • How can I call these functions? Or better where is point I have to add my functions, so they are called when the text in the textbox changes?
    – Andreas
    Jan 9, 2015 at 9:49

1 Answer 1

1

You can still call your background checks if you call them right after the user has updated the textbox content.

private string name;

public string Name 
{
    get
    {
        return name;
    }

    set
    {           
        CheckName(value); // Or whatever are you check functions

        name = value;

        PropertyChanged("Name");
    }
}

I hope this helps.

3
  • I thought of this too. But to do the check i would have to call a function from my database which is not possible at this point of code. To do some character set checks this the right solution.
    – Andreas
    Jan 9, 2015 at 10:12
  • Why can't you call the database function at this point of code?
    – Natan
    Jan 9, 2015 at 10:15
  • I think I messed up some things. I needed this for a new window and I thought I don't need the private member. I just created a public string{get;set;} and passed over the data from my database. I thought that the set; method of the database object is called. And there I can't call a function of the Database. Maybe a bit confusing, but thanks to you because now I know were my fault was.
    – Andreas
    Jan 9, 2015 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.