Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DialogHost not showing Singleton View #3126

Closed
Diyari-Kurdi opened this issue Mar 8, 2023 · 2 comments
Closed

DialogHost not showing Singleton View #3126

Diyari-Kurdi opened this issue Mar 8, 2023 · 2 comments
Labels
bug evaluation required Items is pending review or evaluation by the team

Comments

@Diyari-Kurdi
Copy link

Bug explanation

Hello! I'm using dependency injection and have registered a Usercontrol as a Singleton. The issue I'm facing is that when I open the DialogHost with the Usercontrol as its content, it works for the first time. However, when I repeat the process on a different window and close the new window, the Usercontrol stops showing up on the first window.

Demo.zip

2023-03-08.06-23-00_Trim.mp4

Version

4.8

@Diyari-Kurdi Diyari-Kurdi added bug evaluation required Items is pending review or evaluation by the team labels Mar 8, 2023
@nicolaihenriksen
Copy link
Contributor

@Diyari-Kurdi I don't believe this is an MDIX issue in particular; more a general WPF thing. Registering a UserControl as a singleton and using it multiple places seems like a risky approach to me. My concerns here are about connecting/disconnecting elements in the visual tree when you "move the user control around".

Judging from your video, I am guessing you want it as a singleton to preserve the state across all your DialogHost instances? If that is the case, consider registering a singleton ViewModel instead (e.g. MyUserControlViewModel) which can hold the shared state, and then use a DataTemplate to visualize the view model in each DialogHost. Conceptually this gives you a new instance (possibly cached) of the UserControl every time a DialogHost has its dialog shown, but they will all deal with the same underlying ViewModel which will be automatically set as the UserControl.DataContext. Something along these lines (untested code!):

<ContentControl Content="{Binding ...your singleton view model...}">
  <ContentControl.Resources>
    <DataTemplate DataType="{x:Type local:MyUserControlViewModel}">
      <local:MyUserControl />
    </DataTemplate>
  </ContentControl.Resources>
</ContentControl>

@Diyari-Kurdi
Copy link
Author

@nicolaihenriksen Thank you for your comment! The actual problem is not with the data (ViewModel), but rather to the visual elements. For Example, if a UserControl have some heavy UI Elements, it takes some time to open the DialogHost each time. That's why I've registered it as a Singleton. However, you are correct in saying that I need to disconnect the UserControl when the DialogHost is closing. Thanks again!

private void DialogClosing(object sender, DialogClosingEventArgs eventArgs)
{
    MainDialogHost.DialogContent = null;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug evaluation required Items is pending review or evaluation by the team
Projects
None yet
Development

No branches or pull requests

2 participants