Can I override AppointmentDialogViewModel?

13 posts, 1 answers
  1. chen zili
    chen zili avatar
    9 posts
    Member since:
    Aug 2009

    Posted 19 Apr 2011 Link to this post

    I want to validate the appointment propertys before confirmed it.
    Is it a feasible solution to override AppointmentDialogViewModel some methods  such as OnConfirmed?
    and can i replace ScheduleView's SchedulerDialogs default AppointmentDialogViewModel  with my AppointmentDialogViewModel?


     
  2. Konstantina
    Admin
    Konstantina avatar
    2332 posts

    Posted 22 Apr 2011 Link to this post

    Hi Chen,

    Unfortunately, you cannot change the AppointmentDialogViewModel. However, you can create your own ViewModel and use it via XAML.

    Regards,
    Konstantina
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Conde
    Conde avatar
    1 posts
    Member since:
    Jun 2011

    Posted 20 Jun 2011 Link to this post

    Hi!

    How can I do that? All our ViewModels inherit from a base class, so we have pretty much the same problem... but how can I tell my XAML View to bind to our custom ViewModel? And how can I tell ScheduleView to use this custom ViewModel instead the standard appointment edition dialog?

    Thanks in advance!
  4. Konstantina
    Admin
    Konstantina avatar
    2332 posts

    Posted 23 Jun 2011 Link to this post

    Hi Conde,

    I will prepare a sample project illustrating this, but I will need more time. It will be ready next week. Is this time frame acceptable for you?

    Best wishes,
    Konstantina
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Remy
    Remy avatar
    3 posts
    Member since:
    Nov 2010

    Posted 06 Jul 2011 Link to this post

    Is the sample project available?
  6. Answer
    Konstantina
    Admin
    Konstantina avatar
    2332 posts

    Posted 07 Jul 2011 Link to this post

    Hi all,

    The sample project is attached to the post. In it is illustrated how to get the ViewModel of the EditAppointmentDialog and change it. The main points here are that you need to have a custom dialog. In which you have to hook to its DataContext and through a Command in the buttons to Cancel/Confirm it. Please mind for the NOTE comments in the project.

    Hope this helps. Please let us know if you have further questions.

    Kind regards,
    Konstantina
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

  7. chen zili
    chen zili avatar
    9 posts
    Member since:
    Aug 2009

    Posted 14 Jul 2011 Link to this post

    Thank you for your hard work.
  8. Simon
    Simon avatar
    56 posts
    Member since:
    May 2008

    Posted 28 Jul 2011 Link to this post

    Thanks.
    What's the following code here for? It's never called.
    private void Grid_Loaded(object sender, RoutedEventArgs e)
    {
        ((FrameworkElement)sender).DataContext = new MyDialogViewModel { DialogViewModel = (AppointmentDialogViewModel)((FrameworkElement)sender).DataContext };
    }

    Also 
    public AppointmentDialogViewModel DialogViewModel { get; set; }
    is never used. what was that there for?

    Also if I wanted to add my own property to the view model and bind to it - I presume I add it to the MyViewModel Class. 
    as below...
    public String TestString
    {
        get { return "Some text"; }
    }

    ...which is bound to like this?
    <Grid Grid.Row="3"
          Margin="6">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="My Test" />
            <TextBox Text="{Binding TestString}" />
       </StackPanel>
    </Grid>

    When I do the above - the custom property has no value when I open the Appointment Dialog.

      Can you tell me what I'm doing wrong with the above?

      Perhaps it'd be best to update the example to show how you handle custom properties. Our use case requires properties to be on the ViewModel (for calulated fields etc) and some we need to read via the Appointment class read from the DB (via RIA) (using IAppointment interface which we extend). Can you show both these example please?.We're struggling with this and really need a real world example if possible.

     Also the GenerateApps() method although creating an appointment - does not show on the ScheduleView - can you explain why?

      Thanks Konstantina!
  9. Simon
    Simon avatar
    56 posts
    Member since:
    May 2008

    Posted 28 Jul 2011 Link to this post

    Any luck with this?
  10. Simon
    Simon avatar
    56 posts
    Member since:
    May 2008

    Posted 31 Jul 2011 Link to this post

    Further to this example...I see some problems....

    If you create an appointment by typing directly into the ScheduleView - rather than double clicking to open a dialog, the validation in the MyDialogViewModel class is not called. I can enter appointments with a single character - and bypass the min 3 character validation. This is not good.

    Also it appears with your approach, that you have to put this xaml (below) within a stack panel or grid everywhere you have controls that bind the MyDialogViewModel. Which seems messy as it creates a new ViewModel instance everytime!.We really only want one ViewModel instance created which is initialized once from the server per session.

    <StackPanel.DataContext>
              <local:MyDialogViewModel />
    </StackPanel.DataContext>

    If I put the above xaml in a root Grid or StackPanel so it is only created once - the code below always returns null. 
    var viewModel = ((FrameworkElement)parameter).DataContext as AppointmentDialogViewModel; Even with your approach as implemented, with rigourous testing we sometimes get a null reference exception.

    Also we want our xaml bindings to be with MyViewModel NOT the MyDialogViewModel. It appears that with your approach we can only bind to MyDialogViewModel which is not much help.

    Can you provide another example that shows how to bind XAML to MyViewModel which is a singleton that also allows us to bind to the Occurence values from the AppointmentDialogViewModel (giving us recurring appointment features etc). We have extensively customised the EditAppointment Dialog template and our bindings are to a RIA object on the ViewModel that implements the IAppointment interface, but it's just is not behaving properly right now. I'd imagine there are plenty of others that would need to do this..  

    Thanks
    Simon
  11. Miroslav Nedyalkov
    Admin
    Miroslav Nedyalkov avatar
    1718 posts

    Posted 02 Aug 2011 Link to this post

    Hello Simon,

     The example Konstantina suggested demonstrates how to change the default ViewModel only for the EditAppointment dialog, which is not used when you are editing or creating appointments in inline mode. To add validation for the appointments everywher,e no matter how they are edited or created, you need to implement the IDataErrorInfo interface in your custom appointment. For more information about how to achieve this you may refer to this example.

    Hope this helps.

    All the best,
    Miroslav Nedyalkov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  12. Simon
    Simon avatar
    56 posts
    Member since:
    May 2008

    Posted 02 Aug 2011 Link to this post

    Thanks Miroslav,

    That certainly helps with validation in-line however we still need to find a better way to deal with customising the AppointmentDialogViewModel or replacing it with ours whilst keeping the Telerik ViewModel functionality.

    See my last post above.

    We need an example that uses a Singleton ViewModel - we use the Unity ContainerControlledLifetimeManager, that allows us to add our own properties to bind to from XAML, that also allows us access to the AppointmentDialogViewModel. 

    The current example for the reasons mentioned does not support this.

    Thanks
    Simon
  13. Konstantina
    Admin
    Konstantina avatar
    2332 posts

    Posted 04 Aug 2011 Link to this post

    Hello Simon,

    There are 2 ways to bind a singleton ViewModel in XAML:
    1. As in Silverlight the x:Static of WPF is missing, the standard workaround is to create a non-static object with non-static property, which is returning as result the static property. (This was the initially proposed solution)
    2. Create a normal ViewModel with commands and properties for what you locally need that just interact with the static one, like methods, accessing values, etc.
    Here are some examples of the two approaches:
    1. The MyViewModelProxy is the one which is non-static and has the property which returns as result the static one:

    public MyViewModel MyViewModel { get { return MyViewModel.Current; } }

    And the property in the singleton ViewModel, called MyViewModel, to which is bound the non-static :

    private static MyViewModel current;
            public static MyViewModel Current
            {
                get
                {
                    if (current == null)
                    {
                        current = new MyViewModel();
                    }
                    return current;
                }
            }

    2. In the normal ViewModel (MyViewModelProxy) the following commands are defined:

    public MyViewModelProxy()
           {
               this.AppointmentEditConfirmCommand = new DelegateCommand(this.MyViewModel.OnAppointmentEditConfirmed);
               this.AppointmentEditCancelCommand = new DelegateCommand(this.MyViewModel.OnAppointmentEditCanceled);
           }

    Which interact with the methods in the singleton ViewModel:

    internal void OnAppointmentEditConfirmed(object parameter)
           {
               var viewModel = ((FrameworkElement)parameter).DataContext as AppointmentDialogViewModel;
               if (this.ValidateAppointmentEdit(viewModel))
               {
                   viewModel.Confirm();
               }
           }
           internal void OnAppointmentEditCanceled(object parameter)
           {
               var viewModel = ((FrameworkElement)parameter).DataContext as AppointmentDialogViewModel;
               viewModel.Cancel();
           }

    In XAML you could find the different ways the Command is bound to the buttons according to the approach chosen.

    Hope this clarifies the matter. Pleas find attached a sample project which illustrates both approaches.

    Regards,
    Konstantina
    the Telerik team
    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
Back to Top