Accessing WorkspaceData's DataValue from the VM?

Oct 11, 2011 at 1:03 PM
Edited Oct 12, 2011 at 12:52 PM

I know I can get access to it from the View, but I really need access from the VM and I am not entirely sure if this is possible. I've been reading the documentation for hours and didn't see an answer. Any help is appreciated!

 edit:

I actually fixed it by passing the data object into the WorkspaceContextualData property in the View's codebehind then using the MEFedMVVM Mediator to NotifyColleagues in the setter.  

Coordinator
Oct 13, 2011 at 12:17 PM
Edited Oct 13, 2011 at 12:18 PM

I am not sure I follow you, each WokspaceData object has a constructor that takes in the dataValue as shown below

 

public WorkspaceData(string imagePath,string viewLookupKey, object dataValue, string displayText, bool isCloseable)

And the WokspaceData class also allows access to this  dataValue via a standard property as shown below

 

 

public object DataValue
        {
            get { return dataValue; }
            set
            {
                dataValue = value;
                NotifyPropertyChanged(dataValueArgs);
            }
        }

 

So why cant you just grab the value from the WokspaceData using the DataValue property.

As long as you have access to the WokspaceData instance you can get this value. I guess if you cant see the instance I would use different solution, I would use the following code, which works as the new that was populated with the WokspaceData can be obtained in the VM and inspected using the IWorkspaceAware interface and using the ViewAwareStatusService (there is a demo of something similar to this in the WPF demo with CInch)

The view, note the IWorkSpaceAware that is important, it tells Cinch your View is expecting WorkSpaceData to be supplied by Cinch

 

 

    [ViewnameToViewLookupKeyMetadata("AboutView", typeof(AboutView))]
    public partial class AboutView : UserControl, IWorkSpaceAware
    {
        #region IWorkSpaceAware Members

        /// <summary>
        /// WorkSpaceContextualData Dependency Property
        /// </summary>
        public static readonly DependencyProperty WorkSpaceContextualDataProperty =
            DependencyProperty.Register("WorkSpaceContextualData", typeof(object), typeof(AboutView),
                new FrameworkPropertyMetadata((WorkspaceData)null));

        /// <summary>
        /// Gets or sets the WorkSpaceContextualData property.  
        /// </summary>
        public WorkspaceData WorkSpaceContextualData
        {
            get { return (WorkspaceData)GetValue(WorkSpaceContextualDataProperty); }
            set { SetValue(WorkSpaceContextualDataProperty, value); }
        }

        #endregion

    }

 

Where this view is shown from ViewModelA as follows (not the string "hello" being used as the WorkSpaceData.DataValue

 

 

WorkspaceData workspace2 = new WorkspaceData(@"/CinchV2DemoWPF;component/Images/About.png",
    "AboutView","hello", "About Cinch V2", true);
Views.Add(workspace2);

 

 

Then in the ViewModel that is created for this workspace (by Cinch) (so say AboutViewModel) we would have something like this

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Composition;

using MEFedMVVM.ViewModelLocator;
using Cinch;
using MEFedMVVM.Common;

namespace CinchV2DemoWPF
{

    [ExportViewModel("AboutViewModel")]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class AboutViewModel : ViewModelBase
    {
        private IViewAwareStatus viewAwareStatusService;
        [ImportingConstructor]
        public AboutViewModel(IViewAwareStatus viewAwareStatusService)
        {
            this.viewAwareStatusService = viewAwareStatusService;
            this.viewAwareStatusService.ViewLoaded += ViewAwareStatusService_ViewLoaded;
        }


        private void ViewAwareStatusService_ViewLoaded()
        {

            if (Designer.IsInDesignMode)
                return;

            IWorkSpaceAware workSpaceAwareView =
                            viewAwareStatusService.View as IWorkSpaceAware;
            if (workSpaceAwareView != null)
            {
                string string_Should_Be_HELLO_Value= ((IWorkSpaceAware)workSpaceAwareView).WorkSpaceContextualData.DataValue.ToString();
            }


        }


        

       
    }
}

 

Do you see what I mean. I have just tested this and it work nicely.

 

Of course Mediator works too

Oct 13, 2011 at 12:26 PM

You're right, I hadn't found a proper example of the viewAwareStatusService until an hour or two ago and I essentially came to the same conclusion as you did here. It is more flexible than the Mediator. I think my situation is unique, I am nesting Views inside a main Workspace without actually using more Workspaces to house these views.  When I added them as Workspaces, I had access to the dataValue object like you've shown me here. I just need to work out the Model's lack of INPC, hoping to use the DataWrapper but there are complex data types (I've seen you suggest against using it for them).

Thanks for replying, Cinch is excellent. :)

Coordinator
Oct 13, 2011 at 4:00 PM

Everyones requirements are different, which is why its sometimes hard to answer folk.

 

Yeah the whole DataWrapper<T> was really meant for when you are binding a single value to a TextBox, so you could expose a single Wrapper which had things like : Value, IsReadonly, IsEditable, IsValid, so would reduce the amount of properties in your viewmodel. People misused them a lot, Grrr.

 

I would use them for things like a single primitive like string, int, float, decimal, double etc etc

 

Glad you like Cinch, it was a lot of work, I feel its good. Nice to hear it from others