admin管理员组

文章数量:1292871

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>

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>
Share Improve this question edited Feb 13 at 5:06 Andrew KeepCoding 13.8k2 gold badges20 silver badges37 bronze badges asked Feb 13 at 1:38 kintonkinton 3031 silver badge7 bronze badges 2
  • You could try to set the Opacity property to the ContentDialog. – Jeaninez - MSFT Commented Feb 13 at 5:55
  • Thank you. I tried the Opacity property, but it made the ContentDialog itself translucent. What I want to make translucent is the overlay on top of the main window behind the dialog. – kinton Commented Feb 13 at 9:19
Add a comment  | 

1 Answer 1

Reset to default 1

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,
    };
}
  • You can learn about SmokeLayerBacground on the generic.xaml file.

  • FindAscendant() comes from the CommunityToolkit.WinUI.Extensions NuGet package.

本文标签: winui 3Change Parent Window Overlay Transparency with ContentDialogStack Overflow