(Simple) MVVM at Outlook-AddIn

Jan 6, 2012 at 7:08 PM

Hi there.

first of all I have to say, I'm more and more becoming a big fan of this toolkit of yours.

As I am doing MS Office programming, specifically Outlook, I came across the following situation.

I have to enrich an Outlook appointment with additional data and store it with some of the data of the appointment item in my backend. I'm doing the presentation via a FormRegion offered by Visual Studio.
Let's assume I want to store the subject (part of Outlook) and some additionalText.

I created a UserControl (my View) which is bound to my ViewModel (Properties Subject and AdditionalText). This UserControl is placed in the FormRegion. (Automatically encapsulated in an ElementHost by Visual Studio.)

I identify

two models
-my own (Subject, derived from the appointment's subject / AdditionalText)
-the Outlook model (Subject ...)

two and a half views
-my own
-the Outlook Form displaying the appointment (including the FormRegion)
(-the FormRegion containing my UserControl)

two and a half view models (or controllers or whatsoever)
-my own
-the internal outlook stuff
(-the FormRegion's code behind)

So, to get my ViewModel fed with the subject, (in the FormRegion) I wired up the PropertyChange event to set the ViewModel.Subject property[2]. Nothing evil so far as I guess this is what's done in the background of my UserControl either. But to get the ViewModel, I am exposing it in my UserControl[1].
I'm not quite sleeping well. But I don't know how it could be done better. Is it okay to share my ViewModel among two views (or part of it)?
And assuming I have to intervene in saving the Outlook appointment, the logic can't be part of MyViewModel, because it isn't aware of such a thing like an appointment and it's not the view model for the outlook model. So this must again be coded in the FormRegion, or not?

Thanks in advance. I'd be happy to explain more, if my post was to weird.


[1]
MyView.xaml.cs

        public MyViewModel ViewModel
        {
            get
            {
                MyViewModel vm = (MyViewModel)this.MainGrid.DataContext;
                return vm;
            }
        }

[2]
MyFormRegion.cs

        void appointmentItem_PropertyChange(string Name)
        {
            Outlook.AppointmentItem appointmentItem = (Outlook.AppointmentItem)this.OutlookItem;
            switch (Name)
            {
                case "Subject":
                    this.myUserControl.ViewModel.Subject = appointmentItem.Subject;
                    break;
            }
        }

Jan 19, 2012 at 1:32 PM

Can you give more source examples.  I'm trying to use mvvm light WPF with my outlook plugin and I am unable to link the datacontext to my viewmodel via the locator as the app config xml is not uses as per a normal applicaiton

Jan 19, 2012 at 3:42 PM
paulman wrote:

Can you give more source examples.

Sure, but I did it a bit messy. I ended up referencing the locator in each view with

    <UserControl.Resources>
        <vm:MyModelLocator xmlns:vm="clr-namespace:MyProject.Locators"
                                   x:Key="Locator" />
    </UserControl.Resources>

as the very first thing in <UserControl> and specifying datacontext as usual

DataContext="{Binding Source={StaticResource Locator}, Path=MyViewModel}"

If anybody knows a better way, let us know.

And my initial question still remains unanswered.