winui-3winuiwinui-xaml

Change Parent Window Overlay Transparency with ContentDialog


I want to change the transparency of the overlay displayed on the parent window when the ContentDialog is open. I am using WinUI 3. I have looked into generic.xaml and tried changing a few things, but there has been no effect at all.

I modified the following code, but I did not see any results.

      <Application.Resources>
        <ResourceDictionary>
          <ResourceDictionary.MergedDictionaries>
            <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls"/>
          </ResourceDictionary.MergedDictionaries>
          <!-- Other app resources here -->
    
          <SolidColorBrush x:Key="ContentDialogBackgroundThemeBrush" Color="Red" />
          <SolidColorBrush x:Key="ContentDialogBorderThemeBrush" Color="Red" />
          <SolidColorBrush x:Key="ContentDialogContentForegroundBrush" Color="Red" />
          <SolidColorBrush x:Key="ContentDialogDimmingThemeBrush" Color="Red" />
          <SolidColorBrush x:Key="SystemControlPageBackgroundBaseMediumBrush" Color="red" />
          <SolidColorBrush x:Key="SystemControlPageBackgroundMediumAltMediumBrush" Color="red" />
    
          <ResourceDictionary.ThemeDictionaries>
            <ResourceDictionary x:Key="Default">
              <SolidColorBrush x:Key="ContentDialogDimmingThemeBrush" Color="Red" />
              <SolidColorBrush x:Key="SystemControlPageBackgroundMediumAltMediumBrush" Color="Red" />
              <StaticResource x:Key="ContentDialogLightDismissOverlayBackground" ResourceKey="SystemControlPageBackgroundMediumAltMediumBrush" />
            </ResourceDictionary>
            <ResourceDictionary x:Key="Dark">
              <SolidColorBrush x:Key="ContentDialogDimmingThemeBrush" Color="Red" />
              <SolidColorBrush x:Key="SystemControlPageBackgroundMediumAltMediumBrush" Color="Red" />
              <StaticResource x:Key="ContentDialogLightDismissOverlayBackground" ResourceKey="SystemControlPageBackgroundMediumAltMediumBrush" />
            </ResourceDictionary>
          </ResourceDictionary.ThemeDictionaries>
        </ResourceDictionary>
    
      </Application.Resources>

Solution

  • Try the following on the Opened event:

    private void ContentDialog_Opened(ContentDialog sender, ContentDialogOpenedEventArgs args)
    {
        if (VisualTreeHelper
            .GetOpenPopupsForXamlRoot(this.XamlRoot)
            .FirstOrDefault() is not Popup popup)
        {
            return;
        }
    
        if ((popup.Child as ContentDialog)?.FindAscendant<Canvas>() is not Canvas popupRoot ||
            popupRoot.Children.OfType<Rectangle>().FirstOrDefault(x => x.Name is "SmokeLayerBackground") is not Rectangle smokeLayerBackground)
        {
            return;
        }
    
        // This will change the overlay background.
        smokeLayerBackground.Fill = new SolidColorBrush()
        {
            Color = Colors.SkyBlue,
            Opacity = 0.5,
        };
    }