Question

I am currently using the MEFedMVVM framework to get access to ViewModels and want to know how to get data from another ViewModel currently being used. This is coupled with the use of Cinch.

At present my tab control is defined as below:

<Window.Resources>


    <DataTemplate DataType="{x:Type CinchV2:WorkspaceData}">
        <AdornerDecorator>
            <Border HorizontalAlignment="Stretch" 
                VerticalAlignment="Stretch" 
                CinchV2:NavProps.ViewCreator="{Binding}"/>
        </AdornerDecorator>
    </DataTemplate>


</Window.Resources>

And my Main Window ViewModel is setup in the following way once the View has been loaded:

private void ViewAwareStatusService_ViewLoaded()
    {

        if (Designer.IsInDesignMode)
            return;

        //String imagePath = ConfigurationManager.AppSettings["YourImagePath"].ToString();

        WorkspaceData loginWorkSpace = new WorkspaceData(null, "LoginUserControl", null, "Login", true);
        WorkspaceData aboutWorkspace = new WorkspaceData(null, "About", null, "About", true);
        WorkspaceData viewAlbumsWorkspace = new WorkspaceData(null, "ViewAlbums", null, "View Albums", true);
        WorkspaceData readReviewSelectWorkspace = new WorkspaceData(null, "ReadReviewsSelect", null, "Select Review", true);
        WorkspaceData adminWorkspace = new WorkspaceData(null, "Admin", null, "Admin", true);

        Views.Add(aboutWorkspace);
        Views.Add(loginWorkSpace);
        Views.Add(readReviewSelectWorkspace);
        Views.Add(viewAlbumsWorkspace);


        SetActiveWorkspace(aboutWorkspace);

        UserName = new DataWrapper<string>(this, UserNameChangeArgs);
        UserName.IsEditable = true;
        //UserName.DataValue = ConfigurationManager.AppSettings["UserName"];

        UserRole = new DataWrapper<string>(this, UserNameChangeArgs);
        UserRole.IsEditable = true;
        //UserRole.DataValue = ConfigurationManager.AppSettings["UserType"];
    }

The MainWindow ViewModel inherits the ViewModelBase Class which is configured as below (Cinch Class):

namespace Cinch
{
public abstract class ViewModelBase : INotifyPropertyChanged, ICinchDisposable,  IParentablePropertyExposer
{
    public ViewModelBase();

    public SimpleCommand<object, object> CloseActivePopUpCommand { get; }
    public SimpleCommand<object, object> CloseWorkSpaceCommand { get; }
    public string DisplayName { get; set; }
    public bool IsCloseable { get; set; }
    protected virtual bool ThrowOnInvalidPropertyName { get; }
    public ObservableCollection<WorkspaceData> Views { get; set; }

    public event EventHandler<EventArgs> ActivateRequest;
    public event EventHandler<CloseRequestEventArgs> CloseRequest;
    public event EventHandler<EventArgs> CloseWorkSpace;
    public event Action<string> FocusRequested;
    public event PropertyChangedEventHandler PropertyChanged;

    public void Dispose();
    public Delegate[] GetINPCSubscribers();
    protected void NotifyPropertyChanged(PropertyChangedEventArgs args);
    protected void NotifyPropertyChanged(string propertyName);
    protected virtual void OnDispose();
    public virtual void RaiseActivateRequest();
    public virtual void RaiseCloseRequest(bool? dialogResult);
    public void RaiseFocusEvent(string focusProperty);
    public void SetActiveWorkspace(WorkspaceData viewnav);
    [DebuggerStepThrough]
    [Conditional("DEBUG")]
    public void VerifyPropertyName(string propertyName);
}

Now what I want to be able to do is, from a button click within the loginWorkspace which is a UserControl with a ViewModel, is add the Admin tab to the MainWindow's Views ObservableCollection from the LoginViewModel.

No correct solution

OTHER TIPS

Read up about using the Mediator. You can send a message from your loginviewmodel. Then have the mainwindowview model can listen for the appropriate message.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top