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)); }
public void TestApplyModeSetAndGet() { Panel stackPanel = new StackPanel(); ImplicitStyleManager.SetApplyMode(stackPanel, ImplicitStylesApplyMode.Auto); Assert.AreEqual(ImplicitStyleManager.GetApplyMode(stackPanel), ImplicitStylesApplyMode.Auto); }
/// <summary> /// Helper method that verifies the expected behavior for ApplyMode=Auto. /// In this method's scenario, a StackPanel contains two Buttons: one added right away /// and another one added later. /// </summary> /// <param name="action">Action that sets the source of the styles (element's resources or ResourceDictionaryUri) and the ApplyMode.</param> /// <param name="color">Expected color for both Buttons after calling the Apply method.</param> private void TestAuto(Action <Panel> action, Color color) { Panel stackPanel = new StackPanel(); Button firstButton = new Button(); Button secondButton = new Button(); stackPanel.Children.Add(firstButton); action(stackPanel); bool hasLayoutUpdatedEventBeenRaised = false; stackPanel.LayoutUpdated += (source, args) => hasLayoutUpdatedEventBeenRaised = true; TestAsync( stackPanel, () => { EnqueueConditional(() => hasLayoutUpdatedEventBeenRaised); hasLayoutUpdatedEventBeenRaised = false; }, () => Assert.IsNotNull(firstButton.Style, "firstButton should contain a Style (before the Apply method is called)."), () => stackPanel.Children.Add(secondButton), () => { EnqueueConditional(() => hasLayoutUpdatedEventBeenRaised); hasLayoutUpdatedEventBeenRaised = false; }, () => Assert.IsNotNull(secondButton.Style, "secondButton should contain a Style (before the Apply method is called)."), () => ImplicitStyleManager.Apply(stackPanel), () => Assert.IsNotNull(firstButton.Style, "firstButton should contain a Style (after the Apply method is called)."), () => Assert.IsNotNull(secondButton.Style, "secondButton should contain a Style (after the Apply method is called)."), () => Assert.AreEqual(color, ((SolidColorBrush)firstButton.Foreground).Color, String.Format(CultureInfo.CurrentCulture, "firstButton should have color {0}", color)), () => Assert.AreEqual(color, ((SolidColorBrush)secondButton.Foreground).Color, String.Format(CultureInfo.CurrentCulture, "secondButton should have color {0}", color))); }
public void TestResourceDictionaryUriSetToMissingResourceThrowsException() { Panel stackPanel = new StackPanel(); Uri uri = new Uri("System.Windows.Controls.Testing.Theming;component/ImplicitStyleManager/non-existantresource.xaml", UriKind.Relative); ImplicitStyleManager.SetResourceDictionaryUri(stackPanel, uri); }
public void TestResourceDictionaryUriSetAndGet() { Panel stackPanel = new StackPanel(); Uri uri = new Uri("System.Windows.Controls.Testing.Theming;component/ImplicitStyleManager/OuterStyleResourceDictionary.xaml", UriKind.Relative); ImplicitStyleManager.SetResourceDictionaryUri(stackPanel, uri); Assert.AreSame(ImplicitStyleManager.GetResourceDictionaryUri(stackPanel), uri); }
public void TestNoneResourceDictionaryUri() { TestNone( (stackPanel) => { SetResourceDictionaryUri(stackPanel, innerUri); ImplicitStyleManager.SetApplyMode(stackPanel, ImplicitStylesApplyMode.None); }, Colors.Blue); }
/// <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); }
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)); }
public void TestNoneResources() { TestNone( (stackPanel) => { foreach (Style style in _outerContainerStyles) { stackPanel.Resources.Add(style.TargetType.FullName, style); } ImplicitStyleManager.SetApplyMode(stackPanel, ImplicitStylesApplyMode.None); }, Colors.Red); }
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 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); }); }
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)); }
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.")); }
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)); }
public void TestAutoOneTimeNestedModes() { SubtreeDifferentMode userControl = new SubtreeDifferentMode(); Button thirdButton = new Button { Content = "Third button" }; TestAsync( userControl, () => Assert.AreEqual(Colors.Red, ((SolidColorBrush)userControl.firstButton.Background).Color), () => Assert.AreEqual(Colors.Red, ((SolidColorBrush)userControl.secondButton.Background).Color), () => userControl.secondButton.Content = thirdButton, () => Assert.IsNull(thirdButton.Style), () => ImplicitStyleManager.Apply(userControl.secondButton), () => Assert.AreEqual(Colors.Red, ((SolidColorBrush)userControl.firstButton.Background).Color), () => Assert.AreEqual(Colors.Red, ((SolidColorBrush)userControl.secondButton.Background).Color), () => Assert.AreEqual(Colors.Red, ((SolidColorBrush)thirdButton.Background).Color), () => Assert.IsNotNull(thirdButton.Style)); }
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); }); }
/// <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); }); }
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)); }
/// <summary> /// Helper method that verifies the expected behavior for ApplyMode=None. /// In this method's scenario, a StackPanel contains two Buttons: one added right away /// and another one added later. /// </summary> /// <param name="action">Action that sets the source of the styles (element's resources or ResourceDictionaryUri) and the ApplyMode.</param> /// <param name="color">The color to use for the styles.</param> private void TestNone(Action <Panel> action, Color color) { Panel stackPanel = new StackPanel(); Button firstButton = new Button(); Button secondButton = new Button(); stackPanel.Children.Add(firstButton); action(stackPanel); TestAsync( stackPanel, () => Assert.IsNull(firstButton.Style, "firstButton should *not* contain a Style (before the Apply method is called)."), () => stackPanel.Children.Add(secondButton), () => Assert.IsNull(secondButton.Style, "secondButton should *not* contain a Style (before the Apply method is called)."), () => ImplicitStyleManager.Apply(stackPanel), () => Assert.IsNotNull(firstButton.Style, "firstButton should contain a Style (after the Apply method is called)."), () => Assert.IsNotNull(secondButton.Style, "secondButton should contain a Style (after the Apply method is called)."), () => Assert.AreEqual(color, ((SolidColorBrush)firstButton.Foreground).Color, String.Format(CultureInfo.CurrentCulture, "firstButton should have color {0}", color)), () => Assert.AreEqual(color, ((SolidColorBrush)secondButton.Foreground).Color, String.Format(CultureInfo.CurrentCulture, "secondButton should have color {0}", color))); }
public void TestResourceDictionaryUriSetOnNullElementThrowsException() { Uri uri = new Uri("System.Windows.Controls.Testing.Theming;component/ImplicitStyleManager/OuterStyleResourceDictionary.xaml", UriKind.Relative); ImplicitStyleManager.SetResourceDictionaryUri((FrameworkElement)null, uri); }
private void ApplyStylesManuallyButton_Click(object sender, System.Windows.RoutedEventArgs e) { ImplicitStyleManager.Apply(manualContainer); }
public void TestApplyWithNullFrameworkElementParameter() { ImplicitStyleManager.Apply(null as FrameworkElement); }
public void TestApplyModeGetOnNullElementThrowsException() { ImplicitStyleManager.GetApplyMode((FrameworkElement)null); }
public void TestResourceDictionaryUriGetOnNullElementThrowsException() { ImplicitStyleManager.GetResourceDictionaryUri((FrameworkElement)null); }
/// <summary> /// Associates a resource dictionary uri with an element. /// </summary> /// <param name="element">The element to associate the uri with.</param> /// <param name="uri">The uri of a resource dictionary.</param> public static void SetResourceDictionaryUri(FrameworkElement element, Uri uri) { ImplicitStyleManager.SetResourceDictionaryUri(element, uri); }
public void TestApplyModeSetOnNullElementThrowsException() { ImplicitStyleManager.SetApplyMode((FrameworkElement)null, ImplicitStylesApplyMode.Auto); }