Issue in getiing the view name for showing the dialog in wpf prism

Topics: Prism v2 - WPF 4, Prism v4 - Silverlight 4, Prism v4 - WPF 4
May 29, 2013 at 11:15 AM
Hi,

I was able to design the page into 3 regions , the middle region is dynamically loaded with different views.
The first region has a view , on click of a button I want to show a modal dialog , Currently I am facing an issue in showing that.
Can you please share if you have any links or code so that I can refer and work upon.

I have a View,ViewModel, Model,DAL

The button command is present in the viewmodel. On click of it , I am first getting the regionname and then I am struck when I want to retrieve the View (GetView (typeof(viewname) ,, the reason being ViewModel doesn't have reference of view . Already i created a reference of viewmodel in view and if i try to dao it viceversa circulardependncy occurs. I need to get the viewname .\
Should I change any architecture here. (view,viewmodel,model,DAL)

Need urgent help in this regard.

Regards
Krishna
Developer
May 29, 2013 at 6:23 PM
Hi Krishna,

I am not aware of how your problem with the modal dialog is related to the one with retrieving the view from the region; however, I will try to provide an answer for both of them.

Regarding the first problem (of showing a modal dialog) you have several approaches you can use to show a dialog window. One of them is using the WindowDialogActivationBehavior included in the Stock Trader RI which creates a region that acts as a popup when a view is injected in it. Another approach is to use a PopupWindowAction to show a dialog in response to an InteractionRequest. You can find a modified version a PopupWindowAction in the following blog post:
Regarding the second problem, a possible approach to obtain the view corresponding to your view model from a region is to iterate over the views until you find one that is using the view model as its DataContext. For example, you could implement a logic similar to the following one in the view model in order to obtain the view from a specific region:
object myView = null;

foreach (var view in this.regionManager.Regions["RegionName"].Views)
{
    FrameworkElement element = view as FrameworkElement;
    if (element != null && element.DataContext == this)
    {
        myView = view;
     }
}
I hope this help,

Damian Cherubini
http://blogs.southworks.net/dcherubini
Jun 3, 2013 at 11:22 AM
Hi,
Thanks for your reply.
I tried using the POpupwindow action.

I have a view (Usercontrol ) where it has a pdf, the source is retrieved from DB and via PDFconverter it is binded.Now the issue is
On click of a button , the dialog should open and the source should get binded.

I tried implementing this using the PopUpaction , How can we bind the source to the Popupdialog .
I tried seeting the porperties of viewmodel like ID,name ,area etc and set it to the content property of Notification.
It is not binding ,I see in the output window , Cannot find Viewmodel or proeprty in 'Confirmation' class.

Requirement:'

I want to pass data to the Dialog ,display name, Id etc on dialog along with pdf.

Need your suggestions.
Developer
Jun 3, 2013 at 8:13 PM
Hi,

In my opinion, if you want to pass information from your view model to your popup you should not use a Notification directly, but a class that inherits from Notification and provides you with the correct properties you need to pass and create bindings. As the class would inherit from Notification it can be passed in the InteractionRequest without problems.

In the sample provided with the PopupWindowAction post you can find a couple of examples showing how to pass information to a popup using either a class that inherits from Notification or a separate view model. You can use them as a reference.

I hope this helps,

Damian Cherubini
http://blogs.southworks.net/dcherubini
Oct 16, 2013 at 10:37 AM
Hi ,We were able to develop the app with dialogs using interaction requests .Now the issue by using PopUPwindowAction is

Suppose, I open a dialog from a view that dialog has a grid .Now if we navigate/select any row to the middle of the grid and close the app.The next time I open the dialog should be opened and the first row should be selected.But I see the dialog is not re-initialized again.we tried setting focus or setting a new instance of Viewmodel/Interaction request but doesn't work either.please let us know the solution for this issue.

Regards
Krishna
Oct 16, 2013 at 3:45 PM
Hi chaituatp,

Unfortunately, I wasn't able to reproduce the problem you mention. I would suggest you to verify the following:

  • The ViewModel is created each time the dialog is raised.
An example of a newly popup dialog creation can be seen in the previously linked PopupWindowAction sample on HelloWorldViewModel class:
private void OnRaiseConfirmation()
{
      this.ConfirmationRequest.Raise(
          new Confirmation { Content = "Confirmation Message", Title = "WPF Confirmation" },
          (cb) => { Result = cb.Confirmed ? "The user confirmed" : "The user cancelled"; });
}
Also, it would be hepful If you could provide us with a repro sample of your scenario so we could reproduce the issue and be able to apply a solution.

I hope you find this helpful,

Gabriel Ostrowsky
http://blogs.southworks.net/gostrowsky
Oct 17, 2013 at 11:33 AM
Hi,

Thanks for your reply. Today I found the issue , It was because 'WindowContent ' is set only during the first time the dialog or application launches. After that WindowContent remains same ( Meaning Scrollbar positions , etc ) each and every time the dialog gets opened.

This is how we set the windowcontent in prism during the application launches/dialog launches.,Now the issue is How can we reinitialize it again / create new instance of WindowContent each and everytime the user closes and open the dialog.

<prismInter:InteractionRequestTrigger SourceObject="{Binding PromptABCSnapShot,Mode=OneWay}">
                <UserSnapshot:DialogAction IsModal="False" CenterOverAssociatedObject="False" DialogHeight="800" DialogWidth="1200">
                    <UserSnapshot:DialogAction .WindowContent>
                        <UserSnapshot:AddressDialogView></partnerSnapshot:AddressDialogView>
                    </UserSnapshot:DialogAction .WindowContent>
                </UserSnapshot:DialogAction >
            </prismInter:InteractionRequestTrigger>
need Help in this regard.

Krishna
Oct 17, 2013 at 9:15 PM
Hi Krishna,

I've managed to reproduce your issue successfully. In order to fix it and reinitialize the Popup dialog's information and state, you could think on the AddressDialogView as the Popup window's Shell view where you would define the region that will keep the selected view.

What I mean to say is that you could define the AddressDialogView as a blank view which defines the region that will store the actual view you want to show. Therefore, each time the dialog is shown, you would add a new instance of the View from the container into the popup's region. This way you would show a new instance of the View every time you open the dialog. In order to get the scoped region manager of the dialog, you could Publish it from the DialogAction and handle the View Activation from the Main Window ViewModel before raising the dialog:
this.eventAggregator.GetEvent<ScopedRegionManagerSelectedEvent>().Suscribe(this.SetDialogRegionManager);
...
private void SetDialogRegionManager(IRegionManager dialogRegionManager)
{
    this.dialogRegionManager = dialogRegionManager;
}
private void OnRaiseSelectItem()
{
    this.SelectItemRequest.Raise(
        new SelectItemViewModel { Title = "Items" });
    var activeViews = this.dialogRegionManager.Regions["DialogRegion"].ActiveViews;
    if( activeViews.ElementAt(0) != null )
    {
         this.dialogRegionManager.Regions["DialogRegion"].Remove(activeViews[0]);
    }
    this.dialogRegionManager.Regions["DialogRegion"].Add(container.Resolve<SubDialogView>(), "SubDialogView");
}
Another alternative that would also work is to reinitialize() every property manually on the AddressDialogView when the dialog launches. You could implement this solution by defining an interface on the Dialog's viewModel which declares the reinitialize() method, and calling it before raising the dialog. This reinitialize method would set all the Properties to their default values.

I hope this helps,
Gabriel Ostrowsky
https://blogs.southworks.net/gostrowsky
Oct 24, 2013 at 1:52 PM
Thanks for your reply. I was able to solve it.