-1

I need to get the DataContext of the View set by using ContentSource property of the ModernWindow, Could you please help.I am using MVVM framework with Modern UI. The ViewModel code from where I need to show another window is as follows,

public void ShowPrompt()
{
    this.PromptWindow = ObjectFactory.GetInstance<IPromptWindowViewModel>().Window as ModernWindow;
    this.PromptWindow.Owner = Application.Current.MainWindow;
    this.PWPMainViewModel.PromptWindowsCollection.Add(this.PromptWindow);
    // Here I need to get the DataContext of PromptWindow's Content
    this.PromptWindow.Show();
}

I did some debugging and found that by inherting IContent interface from ModernUI in the 'OnNavigatedTo' event

public void OnNavigatedTo(FirstFloor.ModernUI.Windows.Navigation.NavigationEventArgs e)
{
    IPWPMainViewModel pwpMainViewModel = ObjectFactory.GetInstance<IPWPMainViewModel>();
    pwpMainViewModel.PromptMainsCollection.Add(new ContentControl { Content = e.Content });
    IPromptMainViewModel promptMainViewModel = ((UserControl)e.Content).DataContext as IPromptMainViewModel;
}

Here I am able to get the DataContext of the ModernWindow's Content i.e. of type 'IPromptMainViewModel' but here its very difficult to map/load the views into this ModernWindow as there are multiple instances of views, but I would like to do it in the ViewModel where 'ShowPrompt()' is present as there the Model will be associated with the View correctly so I can map there the views easily.

Thank you.

daniele3004
  • 13,072
  • 12
  • 67
  • 75
  • From where? The view or the viewmodel? Or somewhere else? What have you tried? What errors are you getting, if any? Can you please give more detail - at the moment it's difficult to help you – Charleh Sep 01 '14 at 11:24

1 Answers1

0

To get this done, I set the Content of the ModernWindow by myself (as shown in below code in a method in a ViewModel) without using the ContentSource DependencyProperty, If we use the ContentSource property it will be set for a ModernFrame type by the ModernWindow itself creating its Content instance after Navigation to that View completes in some method in ModernFrame class from ModernUI for WPF by using ModernFrame's Source DependencyProperty.

    public void ShowPrompt()
    {            
        this.PromptWindow = ObjectFactory.GetInstance<IPromptWindowViewModel>().Window as ModernWindow;
        this.PromptWindow.Title = string.Concat("Control ", this.PromptOriginsEntity.PromptOriginsIdentity);
        this.PromptWindow.Tag = this.PromptOriginsEntity.PromptOriginsIdentity;
        this.PromptWindow.Owner = Application.Current.MainWindow;

        // Store Window object in PromptWindowsCollection
        this.PWPMainViewModel.PromptWindowsCollection.Add(this.PromptWindow);
        this.PromptWindow.Show(); // inorder to retrieve the ModernFrame the ModernWindow is to be shown first

        ModernFrame frameContent = (ModernFrame)this.PromptWindow.Template.FindName("ContentFrame", this.PromptWindow);
        UserControl userControl = new UserControl { Content = GetView<IPromptViewModel>(), Tag = this.PromptOriginsEntity.PromptOriginsIdentity };
        frameContent.Content = userControl;
        this.PWPMainViewModel.PromptsCollection.Add(userControl);

        IPromptViewModel promptViewModel = (IPromptViewModel)((IView)userControl.Content).DataContext;
        promptViewModel.PromptEntity.Identity = this.PromptOriginsEntity.PromptOriginsIdentity;
    }

I've uploaded a prototype app at https://wpfmvvmsamples.codeplex.com/SourceControl/latest

Thanks.