0

I have a problem with Prism / WPF custom interaction request popups. The popup renders correctly on first request, but each subsequent popup reuses the same view. This results in TextBlock controls concatenating text, scroll bars not being visible, dynamic data in ItemsControl items not being visible, the popup window having wrong size, etc. Is it possible to force creation of new popup window with each new interaction request or refresh all controls in the popup?

To show popup I am using standard code from PRISM documentation, for example the popup is instantiated as:

PopUpViewModel displayData = reportCreator.GetReport();
this.CustomConfirmationRequest.Raise(displayData, res => { 
 if (res.Confirmed)  
 { ... }
});

where PopUpViewModel inherits Confirmation, IInteractionRequestAware

XAML is:

<prism:InteractionRequestTrigger SourceObject="{Binding CustomConfirmationRequest, Mode=OneWay}">
            <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True" WindowStartupLocation="CenterScreen"  >
                <prism:PopupWindowAction.WindowContent>
                    <popups:SoPopUp/>
                </prism:PopupWindowAction.WindowContent>
            </prism:PopupWindowAction>
        </prism:InteractionRequestTrigger>
lekso
  • 1,731
  • 3
  • 24
  • 46

2 Answers2

5

Instead of

<prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True" WindowStartupLocation="CenterScreen">
  <prism:PopupWindowAction.WindowContent>
    <popups:SoPopUp/>
  </prism:PopupWindowAction.WindowContent>
</prism:PopupWindowAction>

you may use

<prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True" WindowStartupLocation="CenterScreen" WindowContentType = "{x:Type popups:SoPopUp}"/>

When you specify WindowContent, the instance of SoPopUp is created once when this xaml is loaded. It is then reused every time the PopupWindowAction is triggered. If you specify WindowContentType, an instance of SoPopUp is created anew each time the PopupWindowAction is triggered. Note also that DI is used to instantiate SoPopUp so that the SoPopUp constructor may have arguments to be resolved by DI.

F Carefree
  • 15
  • 5
dvorn
  • 3,107
  • 1
  • 13
  • 12
  • 1
    Amazing, thank you. After you replied I also found your discussion on GitHub. Why has this never made its way into PRISM documentation? For everyone else's reference, this explains the issue: https://github.com/PrismLibrary/Prism/issues/432 – lekso May 25 '18 at 13:41
  • @dvorn: could you add a note emphasizing `WindowContentType` vs `WindowContent` and that the former creates new instances each time instead of reusing a single instance? – Haukinger May 25 '18 at 16:52
  • @lekso Current Prism WPF documentation was actually created for version 5 by Microsoft's Patterns and Practices team. Prism WPF has not changed much since then. There were just a few little additions. Most notable change is for DelegateCommand. I myself is a bit shy to write the documentation because English is not my native language. – dvorn Jun 01 '18 at 05:19
0

Your best bet is to create a correct view model that can cope with being reused, as detailed here for example.

You can try to use RegionMemberLifetimeAttribute, but I wouldn't really expect that to help out here as you're not doing navigation...

Haukinger
  • 10,420
  • 2
  • 15
  • 28
  • The problem which I am stuck with is not the data. Updated viewmodel binds to the view and I can see the updated data. The problem is the window controls which keep the state from the first popup request. So if my list in the first popup had two items then the view will render these as two records (using ItemsControl WPF control). If second popup's viewmodel has 4 items then only the first two would render. Those two items will be bound correctly, but the other two are not displayed. This happens with every control in the popup. – lekso May 24 '18 at 08:38
  • Are you sure that you notify all changes to the view? If you don't use an `ObservableCollection` you have to notify the collection property of the popup view model. What you describe sounds like you notify the changes of the first two items, but not the change to the list. – Haukinger May 24 '18 at 11:26
  • please see my comment on dvorn's reply. Scroll to the bottom of the linked page :) – lekso May 25 '18 at 13:42
  • nice find! but I'd really be interested in the reason for the behavior you get when reusing the view model. If you happen to find out, please give me a note – Haukinger May 25 '18 at 16:51