1

I am having trouble converting an eventhandler into a command using MVVM design and the Prism toolkit.

I'm also using the WPToolkit -- the DatePicker. I need to set the ValueChanged event to a command.

Here's my code:

MainPageViewModel

_setDateOne = new DelegateCommand(delegate()
     { });

void picker1_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
{
   using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
{
   if (isf.FileExists("DateOne"))
       isf.DeleteFile("DateOne");
 IsolatedStorageSettings.ApplicationSettings["DateOne"] = e.NewDateTime.Value;
 IsolatedStorageSettings.ApplicationSettings.Save();
}
}

Xaml

<toolkit:DatePicker Name="picker1" ValueChanged="picker1_ValueChanged" Value="{Binding DateOne, ElementName=this, Mode=TwoWay}"/>

I know the XAML is wrong; I don't know how to do it properly, yet.

Sorry, I'm pretty newbie with programming and especially MVVM.

user3007447
  • 380
  • 3
  • 15
  • If you're doing MVVM, don't use the ValueChanged event, rather implement INotifyPropertyChanged in your ViewModel and in the setter of the property you've bound to the DatePickers Value invoke whatever you have planned for the valuechange command. – safetyOtter Mar 06 '14 at 03:45
  • I do have the NotifyPropertyChanged event implemented. The code in ValueChanged is just what I want to happen when the value is changed; not sure how to actually implement it with a command. And also, I need the DateTimeValueChangedEventArgs e in order to know what to set my DateTime to. – user3007447 Mar 06 '14 at 03:48
  • in the setter for DateOne, `set { DateOne = value; CallYourMethodHere(value); }` – safetyOtter Mar 06 '14 at 03:51
  • Since my binding is TwoWay, I don't actually need the event arg, then? – user3007447 Mar 06 '14 at 03:55
  • Correct. You have the value you need in the DateOne property. – safetyOtter Mar 06 '14 at 03:56
  • You might need to add `UpdateSourceTrigger="PropertyChanged"` to your binding, changing it to `{Binding Path=DateOne, ElementName=this, Mode=TwoWay,UpdateSourceTrigger="PropertyChanged}` I'm not near an IDE so I could test this for you. – safetyOtter Mar 06 '14 at 04:06
  • PropertyChanged was not found. :/ The only options for UpdateSourceTrigger are "Default" and "Explicit". – user3007447 Mar 06 '14 at 04:14
  • eww... Looks like you'll have to also do [this](http://stackoverflow.com/questions/4542291/datepicker-selecteddate-not-changing-when-text-is-input) to get this to work with that DatePicker. sorry. – safetyOtter Mar 06 '14 at 04:18
  • Not sure how to implement that :( ...but I will try to figure it out. Thank you!! – user3007447 Mar 06 '14 at 04:27
  • Wait -- users don't actually type in the date, they select it from a dropdown similar to what's already built into Windows Phone -- is this the same thing I'll need? – user3007447 Mar 06 '14 at 04:29
  • dunno, see if you can get it working without it first :P – safetyOtter Mar 06 '14 at 04:31
  • I've been informed that it's bed time. good luck! – safetyOtter Mar 06 '14 at 04:31

1 Answers1

1

No need of ValueChanged event handler here, you can do procedure to save selected date in setter of DateOne :

private DateTime _dateOne;
public DateTime DateOne
{
    get { return _dateOne; }
    set
    {
        _dateOne = value;
        SaveDate(value);
        NotifyPropertyChanged("DateOne");
    }
}

SaveDate(DateTime date)
{
    using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (isf.FileExists("DateOne"))
           isf.DeleteFile("DateOne");
        IsolatedStorageSettings.ApplicationSettings["DateOne"] = date;
        IsolatedStorageSettings.ApplicationSettings.Save();
    }
}

And assuming DataContext of your page has been set properly, binding this way is enough :

<toolkit:DatePicker Name="picker1" Value="{Binding DateOne, Mode=TwoWay}"/>
har07
  • 88,338
  • 12
  • 84
  • 137