views:

48

answers:

2

Hello,

i've made a binding from a TextBox to a Property. If the user write a date-value into the TextBox, it should be automatically corrected. f.e.: 20.01.10 -> 20.01.2010

The correction is done in the propertys set-block:

public String DateOfBirth
    {
      get
      {
        if (patient.DateOfBirth != DateTime.MinValue)
          return patient.DateOfBirth.ToString("dd.MM.yyyy");
        else
          return patient.BirthdayString;
      }
      set
      {
        string dateParsed = ValidateDatePart(value, false, true);
        DateTime date = new DateTime();
        DateTime.TryParse(dateParsed, out date);

        patient.DateOfBirth = date;
        patient.BirthdayString = dateParsed;

        base.OnPropertyChanged("DateOfBirth");
      }
    }

patient.DateOfBirth and patient.BirthdayString are values in the datamodel of my application. The return of birthday as string is needed, because the user should be able to input parts of a birthday, f.e. if the birthday is not fully known like '12.1967'. My Problem is that, if the user input a date like '20.01.10', this date is corrected in the property, but the corrected date ('20.01.2010') is not set to the TextBox. The TextBox contains still '20.01.' Does anybody have an idea, how the TextBox can be updated? The binding mode should be TwoWay by default.

Here's the XAML for the TextBox:

<TextBox Validation.ErrorTemplate="{StaticResource errorTemplate}" Style="{StaticResource NotEmptyTextBox}">
<Binding Path="OrderVM.Patient.DateOfBirth" ValidatesOnDataErrors="True" ValidatesOnExceptions="True" />
</TextBox>
+1  A: 

the answer is allready provided in this post:

http://stackoverflow.com/questions/2454737/silverlight-two-way-data-binding-on-key-up

EDIT: Sorry I misunderstood the question, my new answer is below

You bound your Textbox to the DateOfBirth property of your patient and not to the DateOfBirth property in your ViewModel:

<Binding Path="OrderVM.Patient.DateOfBirth" ... 

needs to be

<Binding Path="OrderVM.DateOfBirth" ...

And add an extra propety to the Binding IsAsync=true to overrule that the PropertyChanged event may be ignored

Wouter Janssens - Xelos bvba
+1  A: 

just try with

Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}"

Kishore Kumar
Yeah this will fire for every keypress and should work for you.
Kelly