public void TestImplicitStyleButton_oneStyleInVisualTree_onStyleInAppResources () { Style style1 = new Style { TargetType = typeof (Button) }; Setter setter = new Setter (FrameworkElement.WidthProperty, 100.0); style1.Setters.Add (setter); Style style2 = new Style { TargetType = typeof (Button) }; setter = new Setter (FrameworkElement.HeightProperty, 100.0); style2.Setters.Add (setter); Button b = new Button (); b.Resources.Add (typeof (Button), style1); Application.Current.Resources.Remove (typeof (Button)); Application.Current.Resources.Add (typeof (Button), style2); Assert.IsTrue (Double.IsNaN (b.Height), "1"); CreateAsyncTest (b, () => { b.UpdateLayout (); Assert.AreEqual (100.0, b.Width, "2"); Assert.IsTrue (Double.IsNaN (b.Height), "3"); Application.Current.Resources.Remove (typeof (Button)); }); }
public static void SetStyle(Button button, Brush unpressedBrush, Brush mouseOverBrush, Brush pressedBrush, Brush unpressedPen, Brush mouseOverPen, Brush pressedPen, double radius, bool bShine) { Initialise("Button1"); ControlTemplate template = null; foreach (Setter setter in style.Setters) { if (setter.Property == Button.TemplateProperty) { template = (ControlTemplate)setter.Value; foreach (Trigger trigger in template.Triggers) { if (trigger.Property == Button.IsMouseOverProperty) { foreach (Setter setter1 in trigger.Setters) { if (setter1.TargetName == "buttonBackground") { setter1.Value = mouseOverBrush; } else if (setter1.TargetName == "contentPresenter") { setter1.Value = mouseOverPen; } } } else if (trigger.Property == Button.IsPressedProperty) { foreach (Setter setter1 in trigger.Setters) { if (setter1.TargetName == "buttonBackground") { setter1.Value = pressedBrush; } else if (setter1.TargetName == "contentPresenter") { setter1.Value = pressedPen; } } } } } } button.Width = button.ActualWidth; button.Height = button.ActualHeight; button.Background = unpressedBrush; button.Foreground = unpressedPen; button.FocusVisualStyle = null; button.Style = style; button.UpdateLayout(); Rectangle rectangle = (Rectangle)button.Template.FindName("buttonBackground", button); rectangle.RadiusX = radius; rectangle.RadiusY = radius; rectangle = (Rectangle)button.Template.FindName("buttonShine", button); rectangle.Visibility = bShine ? Visibility.Visible : Visibility.Hidden; rectangle.RadiusX = radius * 0.67; rectangle.RadiusY = radius * 0.67; }