Configuring overlay background colour for ContentDialog

后端 未结 4 599
星月不相逢
星月不相逢 2020-12-17 06:49

I\'m writing an app for the universal windows platform using the dark theme and I\'ve noticed that although I\'ve correctly set the requested theme to dark when I display a

4条回答
  •  Happy的楠姐
    2020-12-17 07:33

    Try the below code.

        /// 
        /// Set the Overlay background for content Dialog
        /// 
        /// Content Dialog reference
        public static void SetContentDialogOverlay(UIElement subTree)
        {
    
            var hostparent = VisualTreeHelper.GetParent(subTree);
            var rect = FindVisualChild(hostparent);
            rect.Fill = new SolidColorBrush(Colors.Black);
            rect.Opacity = 0.7;
    
        }
    
        /// 
        /// Find the child element from UIContainer
        /// 
        ///  Type
        ///  Dependency Reference 
        /// 
        public static T FindVisualChild(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                    {
                        return (T)child;
                    }
    
                    T childItem = FindVisualChild(child);
                    if (childItem != null) return childItem;
                }
            }
            return null;
        }
    

    Now, Call the above method on your code behind like this----

       // Based upon your access modifier i.e. public/private or protected
        SetContentDialogOverlay(this); 
    

    Here, "this" represent the Content Dialog reference or you can pass the object reference of the ContectDialog.

    Hope, this will help you to change the color of the Overlay. Happy Coding .. :)

提交回复
热议问题