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?
12 Answers, 1 is accepted
Unfortunately, you cannot change the AppointmentDialogViewModel. However, you can create your own ViewModel and use it via XAML.
Regards,
Konstantina
the Telerik team
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!
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
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!
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; }
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!
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
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.
Miroslav Nedyalkov
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
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
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