Microsoft.Phone.Controls.ContextMenu.SetDefaultStyle C# (CSharp) Method

SetDefaultStyle() private method

Set up the background and border default styles
private SetDefaultStyle ( ) : void
return void
        private void SetDefaultStyle()
        {
            // These styles are not defined in the XAML because according to spec,
            // the background color should be white (opaque) in Dark Theme and black (opaque) in Light Theme.
            // There are no StaticResource brushes that have this property (the black is transparent).
            // We define these in code, because we need to check the current theme to define the colors.

            SolidColorBrush backgroundBrush;
            SolidColorBrush borderBrush;
            if (DesignerProperties.IsInDesignTool || Application.Current.Resources.IsDarkThemeActive())
            {
                backgroundBrush = new SolidColorBrush(Colors.White);
                borderBrush = new SolidColorBrush(Colors.Black);
            }
            else
            {
                backgroundBrush = new SolidColorBrush(Colors.Black);
                borderBrush = new SolidColorBrush(Colors.White);
            }

            Style newStyle = new Style(typeof(ContextMenu));

            Setter setterBackground = new Setter(ContextMenu.BackgroundProperty, backgroundBrush);
            Setter settterBorderBrush = new Setter(ContextMenu.BorderBrushProperty, borderBrush);

            if (null == Style)
            {
                newStyle.Setters.Add(setterBackground);
                newStyle.Setters.Add(settterBorderBrush);
            }
            else
            {
                // Merge the currently existing style with the new styles we want
                bool foundBackground = false;
                bool foundBorderBrush = false;

                foreach (Setter s in Style.Setters)
                {
                    if (s.Property == ContextMenu.BackgroundProperty)
                    {
                        foundBackground = true;
                    }
                    else if (s.Property == ContextMenu.BorderBrushProperty)
                    {
                        foundBorderBrush = true;
                    }

                    newStyle.Setters.Add(new Setter(s.Property, s.Value));
                }

                if (!foundBackground)
                {
                    newStyle.Setters.Add(setterBackground);
                }
                if (!foundBorderBrush)
                {
                    newStyle.Setters.Add(settterBorderBrush);
                }
            }

            Style = newStyle;
        }