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> /// 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))); }
/// <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); }
/// <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 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)); }
/// <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))); }
private void ApplyStylesManuallyButton_Click(object sender, System.Windows.RoutedEventArgs e) { ImplicitStyleManager.Apply(manualContainer); }
public void TestApplyWithNullFrameworkElementParameter() { ImplicitStyleManager.Apply(null as FrameworkElement); }