Hello,
I'm trying to create a style for a button, especially to change the IsMouseOver style (it must be overrided through a control template). I'm trying this code (I have to use c#, no xaml), but the button is only turning to yellow, IsMouseOver is not doing anything. Am I missing something ?
Here is the code :
private void CreateElement(int i){
UIElementOut[i] = new Button(); var uiElement = (Button)UIElementOut[i]; uiElement.Width = 100; uiElement.Height = 100; uiElement.VerticalAlignment = VerticalAlignment.Center; uiElement.HorizontalAlignment = HorizontalAlignment.Center; uiElement.Content = TextIn[i]; uiElement.FontFamily = new FontFamily(FFontInput[i]); uiElement.FontSize = Convert.ToDouble(FontSizeIn[i]); Style MyButtonStyle = new Style(); ControlTemplate templateButton = new ControlTemplate(typeof(Button)); FrameworkElementFactory elemFactory = new FrameworkElementFactory(typeof(Button)); elemFactory.Name = "myButton"; elemFactory.SetValue(Button.BackgroundProperty, Brushes.Yellow); templateButton.VisualTree = elemFactory; elemFactory.AppendChild(new FrameworkElementFactory(typeof(ContentPresenter))); Trigger templateTrigger = new Trigger { Property = Button.IsPressedProperty, Value = true }; templateTrigger.Setters.Add(new Setter { Property = Button.ForegroundProperty, Value = Brushes.Violet }); templateTrigger.Setters.Add(new Setter { Property = Button.BackgroundProperty, Value = Brushes.Red }); templateButton.Triggers.Add(templateTrigger); Setter setter = new Setter { TargetName = "myButton", Property = Button.TemplateProperty, Value = templateButton }; MyButtonStyle.Setters.Add(setter); uiElement.Style = MyButtonStyle; uiElement.Template = templateButton;
UIElementOut[i] = new Button();
var uiElement = (Button)UIElementOut[i];
uiElement.Width = 100;
uiElement.Height = 100;
uiElement.VerticalAlignment = VerticalAlignment.Center;
uiElement.HorizontalAlignment = HorizontalAlignment.Center;
uiElement.Content = TextIn[i];
uiElement.FontFamily = new FontFamily(FFontInput[i]);
uiElement.FontSize = Convert.ToDouble(FontSizeIn[i]);
Style MyButtonStyle = new Style();
ControlTemplate templateButton = new ControlTemplate(typeof(Button));
FrameworkElementFactory elemFactory = new FrameworkElementFactory(typeof(Button));
elemFactory.Name = "myButton";
elemFactory.SetValue(Button.BackgroundProperty, Brushes.Yellow);
templateButton.VisualTree = elemFactory;
elemFactory.AppendChild(new FrameworkElementFactory(typeof(ContentPresenter)));
Trigger templateTrigger = new Trigger { Property = Button.IsPressedProperty, Value = true };
templateTrigger.Setters.Add(new Setter { Property = Button.ForegroundProperty, Value = Brushes.Violet });
templateTrigger.Setters.Add(new Setter { Property = Button.BackgroundProperty, Value = Brushes.Red });
templateButton.Triggers.Add(templateTrigger);
Setter setter = new Setter { TargetName = "myButton", Property = Button.TemplateProperty, Value = templateButton };
MyButtonStyle.Setters.Add(setter);
uiElement.Style = MyButtonStyle;
uiElement.Template = templateButton;
}