Beispiel #1
0
        public void TestApplyModeSetAndGet()
        {
            Panel stackPanel = new StackPanel();

            ImplicitStyleManager.SetApplyMode(stackPanel, ImplicitStylesApplyMode.Auto);
            Assert.AreEqual(ImplicitStyleManager.GetApplyMode(stackPanel), ImplicitStylesApplyMode.Auto);
        }
Beispiel #2
0
        public void TestDifferentElements()
        {
            Button     parentButton     = new Button();
            StackPanel parentStackPanel = new StackPanel();
            StackPanel childStackPanel  = new StackPanel();

            parentStackPanel.Children.Add(childStackPanel);
            childStackPanel.Children.Add(parentButton);

            SetResourceDictionaryUri(parentButton, innerUri);
            ImplicitStyleManager.SetApplyMode(childStackPanel, ImplicitStylesApplyMode.OneTime);

            Button childButton = new Button();

            bool hasLayoutUpdatedEventFired = false;

            parentStackPanel.LayoutUpdated += (source, args) => hasLayoutUpdatedEventFired = true;

            TestAsync(
                parentStackPanel,
                () => { EnqueueConditional(() => hasLayoutUpdatedEventFired); hasLayoutUpdatedEventFired = false; },
                () => Assert.AreEqual(Colors.Blue, ((SolidColorBrush)parentButton.Foreground).Color),
                () => parentButton.Content = childButton,
                () => { EnqueueConditional(() => hasLayoutUpdatedEventFired); hasLayoutUpdatedEventFired = false; },
                () => Assert.AreEqual(Colors.Black, ((SolidColorBrush)childButton.Foreground).Color),
                () => ImplicitStyleManager.Apply(parentStackPanel),
                () => Assert.AreEqual(Colors.Blue, ((SolidColorBrush)childButton.Foreground).Color));
        }
        /// <summary>
        /// Applies the style.
        /// </summary>
        /// <param name="uri">The URI of the theme used.</param>
        internal void ApplyStyle(Uri uri)
        {
            ImplicitStyleManager.SetResourceDictionaryUri(this, uri);
            ImplicitStyleManager.SetApplyMode(this, ImplicitStylesApplyMode.OneTime);
            ImplicitStyleManager.Apply(this);

            // explicitly set content of expander.
            ImplicitStyleManager.SetApplyMode(expander.Content as FrameworkElement, ImplicitStylesApplyMode.OneTime);
            ImplicitStyleManager.Apply(expander.Content as FrameworkElement);
        }
Beispiel #4
0
 public void TestNoneResourceDictionaryUri()
 {
     TestNone(
         (stackPanel) =>
     {
         SetResourceDictionaryUri(stackPanel, innerUri);
         ImplicitStyleManager.SetApplyMode(stackPanel, ImplicitStylesApplyMode.None);
     },
         Colors.Blue);
 }
        public void TestApplyStyleInLocalResourcesNone()
        {
            BasicScenario userControl = new BasicScenario();

            ImplicitStyleManager.SetApplyMode(userControl.panel, ImplicitStylesApplyMode.None);

            TestAsync(
                userControl,
                () => Assert.IsInstanceOfType(userControl.btn.Foreground, typeof(SolidColorBrush)),
                () => Assert.AreEqual(Colors.Black, ((SolidColorBrush)userControl.btn.Foreground).Color),
                () => Assert.IsNull(userControl.btn.Style));
        }
Beispiel #6
0
 public void TestNoneResources()
 {
     TestNone(
         (stackPanel) =>
     {
         foreach (Style style in _outerContainerStyles)
         {
             stackPanel.Resources.Add(style.TargetType.FullName, style);
         }
         ImplicitStyleManager.SetApplyMode(stackPanel, ImplicitStylesApplyMode.None);
     },
         Colors.Red);
 }
Beispiel #7
0
 public void TestNoneBothStyleSourcesSimultaneously()
 {
     TestNone(
         (stackPanel) =>
     {
         foreach (Style style in _outerContainerStyles)
         {
             stackPanel.Resources.Add(style.TargetType.FullName, style);
         }
         SetResourceDictionaryUri(stackPanel, innerUri);
         ImplicitStyleManager.SetApplyMode(stackPanel, ImplicitStylesApplyMode.None);
     },
         Colors.Blue);
 }
        /// <summary>
        /// Initialize ImplicitStyleManager once the visual tree is ready.
        /// </summary>
        /// <param name="sender">The UserControl.</param>
        /// <param name="e">Event arguments.</param>
        private void OnLayoutUpdated(object sender, EventArgs e)
        {
            // Only apply once
            LayoutUpdated -= OnLayoutUpdated;

            // ImplicitStyleManager is design to only style controls in the
            // namescope it was defined in.  Since user controls create new
            // namescopes, the ImplicitStyleManager acting on the Theme controls
            // will not style the controls in the AllControls user control.  By
            // applying ImplicitStyleManager to the root of the user control
            // (without giving it a theme to use), it will walk up the visual
            // tree to the Theme control and use its styles.
            ImplicitStyleManager.SetApplyMode(Root, ImplicitStylesApplyMode.OneTime);
            ImplicitStyleManager.Apply(Root);
        }
        public void TestApplyStyleInLocalResourcesAuto()
        {
            BasicScenario userControl = new BasicScenario();
            Button        childButton = new Button();

            ImplicitStyleManager.SetApplyMode(userControl.panel, ImplicitStylesApplyMode.Auto);

            TestAsync(
                userControl,
                () => Assert.IsInstanceOfType(userControl.btn.Foreground, typeof(SolidColorBrush)),
                () => Assert.AreEqual(Colors.Red, ((SolidColorBrush)userControl.btn.Foreground).Color),
                () => Assert.IsNotNull(userControl.btn.Style),
                () => userControl.btn.Content = childButton,
                () => Assert.AreEqual(Colors.Red, ((SolidColorBrush)childButton.Foreground).Color),
                () => Assert.IsNotNull(childButton.Style));
        }
        public void TestApplicationResources()
        {
            AddApplicationResources(Colors.Green);

            Button btn = new Button {
                Content = "Hello"
            };

            ImplicitStyleManager.SetApplyMode(btn, ImplicitStylesApplyMode.Auto);

            TestAsync(
                btn,
                () => Assert.IsInstanceOfType(btn.Foreground, typeof(SolidColorBrush)),
                () => Assert.AreEqual(Colors.Green, ((SolidColorBrush)btn.Foreground).Color),
                () => Application.Current.Resources.Clear());
        }
        public void TestApplicationResourceDictionaryUri()
        {
            SetApplicationResourceDictionaryUri(innerUri);

            Button btn = new Button {
                Content = "Hello"
            };

            ImplicitStyleManager.SetApplyMode(btn, ImplicitStylesApplyMode.Auto);

            TestAsync(
                btn,
                () => Assert.IsInstanceOfType(btn.Foreground, typeof(SolidColorBrush)),
                () => Assert.AreEqual(Colors.Blue, ((SolidColorBrush)btn.Foreground).Color),
                () => SetApplicationResourceDictionaryUri(null));
        }
Beispiel #12
0
 public void TestNestedContainersOneTimeResourceDictionaryUri()
 {
     TestStylesAreAppliedToNestedContainersOnceProperly <StackPanel>(
         (outerPanel) =>
     {
         Uri outerResourceDictionaryLocation = new Uri("System.Windows.Controls.Testing.Theming;component/ImplicitStyleManager/OuterStyleResourceDictionary.xaml", UriKind.Relative);
         ImplicitStyleManager.SetApplyMode(outerPanel, ImplicitStylesApplyMode.OneTime);
         SetResourceDictionaryUri(outerPanel, outerResourceDictionaryLocation);
     },
         (innerPanel) =>
     {
         Uri innerResourceDictionaryLocation = innerUri;
         ImplicitStyleManager.SetApplyMode(innerPanel, ImplicitStylesApplyMode.OneTime);
         SetResourceDictionaryUri(innerPanel, innerResourceDictionaryLocation);
     });
 }
Beispiel #13
0
        public virtual void TestItemsInAnItemsControlWithAnItemsPanelCanBeStyled()
        {
            ListBox listBox = new ListBox();

            listBox.ItemsPanel  = App.Current.Resources["itemsPanelTemplate"] as ItemsPanelTemplate;
            listBox.ItemsSource = new[] { 2, 3, 5 };

            ImplicitStyleManager.SetApplyMode(listBox, ImplicitStylesApplyMode.OneTime);
            SetResourceDictionaryUri(listBox, listBoxUri);

            bool hasLayoutUpdatedEventFired = false;

            TestAsync(
                listBox,
                () => { EnqueueConditional(() => hasLayoutUpdatedEventFired); hasLayoutUpdatedEventFired = false; },
                () => Assert.IsNotNull(listBox.GetVisualDescendents().OfType <ListBoxItem>().First().Style));
        }
Beispiel #14
0
        public void TestTogglingOneTimeToAuto()
        {
            Setter outerButtonForegroundSetter = new Setter();

            outerButtonForegroundSetter.Property = Control.ForegroundProperty;
            outerButtonForegroundSetter.Value    = new SolidColorBrush(Colors.Red);

            Style outerButtonStyle = new Style {
                TargetType = typeof(Button)
            };

            outerButtonStyle.Setters.Add(outerButtonForegroundSetter);

            Panel outerPanel = new StackPanel();

            outerPanel.Name = "outerPanel";
            outerPanel.Resources.Add(outerButtonStyle.TargetType.FullName, outerButtonStyle);

            Button styledButton = new Button {
                Content = "Styled button."
            };
            Button unstyledButton = new Button {
                Content = "Unstyled button."
            };
            Button newStyledButton = new Button {
                Content = "Unstyled button."
            };
            bool hasLayoutUpdatedEventFired = false;

            outerPanel.LayoutUpdated += (source, args) => hasLayoutUpdatedEventFired = true;

            TestAsync(
                outerPanel,
                () => ImplicitStyleManager.SetApplyMode(outerPanel, ImplicitStylesApplyMode.OneTime),
                () => outerPanel.Children.Add(styledButton),
                () => { EnqueueConditional(() => hasLayoutUpdatedEventFired); hasLayoutUpdatedEventFired = false; },
                () => Assert.IsNotNull(styledButton.Style, "Styled button has been set correctly."),
                () => outerPanel.Children.Add(unstyledButton),
                () => Assert.IsNull(unstyledButton.Style, "Newly added button has not been styled."),
                () => ImplicitStyleManager.SetApplyMode(outerPanel, ImplicitStylesApplyMode.Auto),
                () => { EnqueueConditional(() => hasLayoutUpdatedEventFired); hasLayoutUpdatedEventFired = false; },
                () => outerPanel.Children.Add(newStyledButton),
                () => { EnqueueConditional(() => hasLayoutUpdatedEventFired); hasLayoutUpdatedEventFired = false; },
                () => Assert.IsNotNull(unstyledButton.Style, "Previously added unstlyed button is now styled after mode has been toggled to Auto."),
                () => Assert.IsNotNull(newStyledButton.Style, "Newly added button is now styled because mode is Auto."));
        }
Beispiel #15
0
 public void TestNestedContainersOneTimeResources()
 {
     TestStylesAreAppliedToNestedContainersOnceProperly <StackPanel>(
         (outerPanel) =>
     {
         foreach (Style style in _outerContainerStyles)
         {
             outerPanel.Resources.Add(style.TargetType.FullName, style);
         }
         ImplicitStyleManager.SetApplyMode(outerPanel, ImplicitStylesApplyMode.OneTime);
     },
         (innerPanel) =>
     {
         foreach (Style style in _innerContainerStyles)
         {
             innerPanel.Resources.Add(style.TargetType.FullName, style);
         }
         ImplicitStyleManager.SetApplyMode(innerPanel, ImplicitStylesApplyMode.OneTime);
     });
 }
Beispiel #16
0
        /// <summary>
        /// Tests that styles are applied correctly when multiple containers
        /// are nested.  The outer container contains styles that apply to a
        /// button and a text block.  It contains a button, a text block, and
        /// another container.  The inner container contains styles that apply
        /// to a button.  It also contains a button and text block.  The test
        /// verifies that the button and text blocks in the outer container are
        /// set to use the styles defined by the outer container.  It also
        /// confirms that the style of the button in the inner container is set
        /// to the style specified by the inner container but that the text
        /// block in the inner container is set to the style of the outer
        /// container because the inner container does not override that style
        /// with a style of its own.
        /// </summary>
        /// <typeparam name="T">The type of panel to use.</typeparam>
        /// <param name="styleOuterContainerAction">An action that
        /// associates a text block and button style with the outer container.
        /// </param>
        /// <param name="styleInnerContainerAction">An action that
        /// associates a text block style with the inner container.</param>
        public void TestStylesAreAppliedProperlyToNestedContainersWhenNotSetToApplyOnce <T>(Action <T> styleOuterContainerAction, Action <T> styleInnerContainerAction)
            where T : Panel, new()
        {
            T      outerStackPanel = new T();
            Button outerButton     = new Button {
                Content = "This is an outer button"
            };
            TextBlock outerTextBlock = new TextBlock {
                Text = "This is an outer text block"
            };
            T      innerStackPanel = new T();
            Button innerButton     = new Button {
                Content = "This is a inner button"
            };
            TextBlock innerTextBlock = new TextBlock {
                Text = "This is an inner TextBlock"
            };

            ImplicitStyleManager.SetApplyMode(outerStackPanel, ImplicitStylesApplyMode.Auto);
            styleOuterContainerAction(outerStackPanel);

            ImplicitStyleManager.SetApplyMode(innerStackPanel, ImplicitStylesApplyMode.Auto);
            styleInnerContainerAction(innerStackPanel);

            TestAsync(
                outerStackPanel,
                () => outerStackPanel.Children.Add(outerButton),
                () => outerStackPanel.Children.Add(outerTextBlock),
                () => outerStackPanel.Children.Add(innerStackPanel),
                () => innerStackPanel.Children.Add(innerButton),
                () => innerStackPanel.Children.Add(innerTextBlock),
                () =>
            {
                Assert.IsNotNull(outerButton.Style);
                Assert.IsNotNull(outerTextBlock.Style);
                Assert.IsNotNull(innerButton.Style);
                Assert.IsNotNull(innerTextBlock.Style);
                Assert.AreNotSame(outerButton.Style, innerButton.Style);
                Assert.AreSame(outerTextBlock.Style, innerTextBlock.Style);
            });
        }
Beispiel #17
0
        public virtual void TestDefaultStyleKeyIsAppliedToDerivedClasses()
        {
            DerivedButton derivedButton = new DerivedButton();
            Button        regularButton = new Button();
            StackPanel    stackPanel    = new StackPanel();

            stackPanel.Children.Add(derivedButton);
            stackPanel.Children.Add(regularButton);

            ImplicitStyleManager.SetApplyMode(stackPanel, ImplicitStylesApplyMode.OneTime);
            SetResourceDictionaryUri(stackPanel, innerUri);
            bool hasLayoutUpdatedEventFired = false;

            stackPanel.LayoutUpdated += (source, args) => hasLayoutUpdatedEventFired = true;

            TestAsync(
                stackPanel,
                () => { EnqueueConditional(() => hasLayoutUpdatedEventFired); hasLayoutUpdatedEventFired = false; },
                () => Assert.IsNotNull(regularButton),
                () => Assert.AreEqual(regularButton.Style, derivedButton.Style));
        }
Beispiel #18
0
 public void TestApplyModeSetOnNullElementThrowsException()
 {
     ImplicitStyleManager.SetApplyMode((FrameworkElement)null, ImplicitStylesApplyMode.Auto);
 }