Created
November 13, 2019 16:43
-
-
Save abodalevsky/57bd46bb1bc730c22847a0197cbc6ee7 to your computer and use it in GitHub Desktop.
WPF Create Visual Style
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Style GetDarkStyleProgrammatically() | |
{ | |
var fef = new FrameworkElementFactory(typeof(Rectangle)); | |
fef.SetValue(Rectangle.StrokeThicknessProperty, 3.0); | |
fef.SetValue(Rectangle.StrokeProperty, new BrushConverter().ConvertFrom("#00008b")); | |
fef.SetValue(Rectangle.SnapsToDevicePixelsProperty, true); | |
var template = new ControlTemplate { VisualTree = fef }; | |
var setter = new Setter(Control.TemplateProperty, template); | |
var style = new Style(typeof(IFrameworkInputElement)); | |
style.Setters.Add(setter); | |
return style; | |
} | |
private Style GetDarkStyleFromXaml() | |
{ | |
var xaml = "<ControlTemplate><Rectangle StrokeThickness=\"3\" Stroke=\"Blue\" SnapsToDevicePixels=\"true\"/></ControlTemplate>"; | |
MemoryStream sr = new MemoryStream(Encoding.ASCII.GetBytes(xaml)); | |
ParserContext pc = new ParserContext(); | |
pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation"); | |
pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml"); | |
var template = (ControlTemplate)XamlReader.Load(sr, pc); | |
var setter = new Setter(Control.TemplateProperty, template); | |
var style = new Style(typeof(IFrameworkInputElement)); | |
style.Setters.Add(setter); | |
return style; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment