2
votes

I want the user to be able to enter e.g. 310312 and have the text property of a datepicker updated to 31/03/12 automatically; I've bound the datepicker to a view model 'Date' property as below.

With WPF4.0, binding now automatically performs a get after a set (no need for INotifyPropertyChanged); this is happening in the code below but although the 'get' date field value is the correct '31/03/12', the datepicker text property is not updating, and remains at 310312 (NB UpdateSourceTrigger=PropertyChanged).

The textbox property does change (e.g. where the set code, not shown, converts to Uppercase)

I would really appreciate some pointers as to why this is.

       <Grid>
        <DatePicker Text="{Binding Path=Date,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
            Height="25" HorizontalAlignment="Left" Name="datePicker1"/>
        <TextBox Text="{Binding Path=State,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  Height="23" HorizontalAlignment="Left"  Name="textBox1" />
    </Grid>

        private string date;
        public string Date
        {
            get
            {
                return date;
            }
            set
            {
                if (value != null)
                {
                    Regex abbreviatedDateFormat = new Regex(@"\A\d{6}\Z");
                    if (abbreviatedDateFormat.IsMatch(value))
                    {
                        value = value.Insert(2, "/");
                        value = value.Insert(5, "/");
                    }
                }

                date = value;                    
            }
        }
1
I want to achieve the same. Did you find any solution? – mosquito87

1 Answers

0
votes

This isn't a definite answer, but I wanted to post code and have it formatted, so I placed it here.

This has intrigued me so I have been playing around with a simple test project. Here's is my code:

MainWindow.xaml

<Window x:Class="datepickertest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" x:Name="Me">
   <StackPanel>
      <DatePicker Name="datePicker" Text="{Binding ElementName=Me, Path=Date, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

      <TextBlock Text="{Binding ElementName=datePicker, Path=Text}"/>
      <TextBlock Text="{Binding ElementName=datePicker, Path=SelectedDate}"/>
      <TextBlock Text="{Binding ElementName=Me, Path=Date}"/>
   </StackPanel>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
  public MainWindow()
  {
     Date = "030912";
     InitializeComponent();         
  }

  private string date;
  public string Date
  {
     get { return date; }
     set
        {
            if (value != null)
            {
                Regex abbreviatedDateFormat = new Regex(@"\A\d{6}\Z");
                if (abbreviatedDateFormat.IsMatch(value))
                {
                    value = value.Insert(2, "/");
                    value = value.Insert(5, "/");
                }
            }

            date = value;                    
        }
  }

}

What I'm noticing is the input for the DateTime control does not understand date formats if they are not separated with a '/' character. I bound the Text property to a TextBlock like you and I can see it updating the TextBlock character by character and it slashes when the Regex is matched, but the actual input box value is not updating(regardless of UpdateSourceTrigger) until a valid date is provided and the enter key is pressed. It seems to me this may be the desired behavior of the control.

I know the control bases its format off the current Thread's culture info, but following this link http://social.msdn.microsoft.com/Forums/en/wpf/thread/f065bcda-a5df-4fd1-bd29-3d0186245c8c, I was still unable to find a solution that would work for you.