Search code examples
c#xamluwpmarkdown

Visualize ContentDialog at full width in the UWP Framework from C# code


I have an UWP application where I dynamically show a ContentDialog when certain buttons are tapped. The ContentDialog will contain markdown text that is dynamically loaded from a configuration file based on which button was clicked. The markdown text contains also references to images (just for information these images' width is 2480 pixels)

private async void ImageHelp_Tapped(object sender, TappedRoutedEventArgs e)
{
try
{

    MarkdownTextBlock markdownTextBlock = new MarkdownTextBlock();
    
    // BASED ON SENDER (button) IT WILL BE DIFFERENT
    markdownTextBlock.Text = ReadMarkDownTextFromConfig(sender); 

    markdownTextBlock.Background = new SolidColorBrush(Colors.Transparent);


    ScrollViewer sv = new ScrollViewer();
    sv.HorizontalScrollMode = ScrollMode.Auto;
    sv.VerticalScrollMode = ScrollMode.Enabled;
    sv.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
    sv.ZoomMode = ZoomMode.Enabled;               
    sv.Content = markdownTextBlock;

    ContentDialog notifyDialog = new ContentDialog
    {
        Content = sv,
        CloseButtonText = "Ok"                    
    };

    await notifyDialog.ShowAsync();
}
catch (Exception ex) { MessageDialog msg = new MessageDialog(ex.Message, "Error"); _ = msg.ShowAsync(); }
}

What I want is that the ContentDialog is visualized at "full width", I have tried setting the width of the ContentDialog but that didn't helped much. I have tried this :

 notifyDialog.MaxWidth = Window.Current.Bounds.Width;

And this:

 notifyDialog.Width = Window.Current.Bounds.Width;

Even if I tried these the actual width of the ContentDialog is half of the Window's width. Even if I set it to a very large number it remains the same


Solution

  • In addition to @Adam Mcmahon's solution about using Popup, an alternative way is to change the default MaxWidth of the ContentDialog. The default ContentDialog MaxWidth is 584 that was setting in the generic.xaml file. To override it, you need to add it in the App.xaml like this:

     <Application.Resources>
        <x:Double x:Key="ContentDialogMaxWidth">5000</x:Double>
    </Application.Resources>