欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WPF通过c#码实现Style样式

程序员文章站 2022-07-13 22:54:19
...

**

WPF通过c#码实现Style样式

**
如何在wpf的c#代码中实现简单样式?而不是通过xaml进行实现?

xaml其实也是在实现具体的类,xaml中能写出来的,通过c#代码肯定也能写出来,有的时候我们需要在viewmodel等代码中实现,简单的代码实现如下:

System.Windows.Style style = new System.Windows.Style(typeof(DataGridCell)) { };
Setter snap = new Setter() { Property = Control.SnapsToDevicePixelsProperty,Value = true };
Setter boeTickness = new Setter() { Property = Control.BorderThicknessProperty, Value = new Thickness(0.1,0,0,0.1) };
Setter borderBrush = new Setter() {Property = Control.BorderBrushProperty,Value = "#FFE8E8E8" };
Setter hor = new Setter() { Property = FrameworkElement.HorizontalAlignmentProperty, Value = HorizontalAlignment.Center };
Setter ver = new Setter() { Property = FrameworkElement.VerticalAlignmentProperty, Value = VerticalAlignment.Center };
style.Setters.Add(hor);
style.Setters.Add(ver);
相关标签: wpf相关 wpf c#