前言:随着wpf的发展,越来越多的开源控件库出来,像比较优秀免费好用的HandyControl,MaterialDesign,收费的代表DevExpress,虽然这些控件库用起来非常的方便,尤其收费的devexpress基本包含了所有你想要的样子,专门为企业大项目服务,但是与此同时也同样产生一个问题,你有没有想过,加入没有这些控件库怎么办,你会用但是你知道底层的样式结构么?出于这点,我选择之后遇到各种控件,有比较复杂一点的样式,我都尝试自己手写一份,这篇作为开章,以此记录下!
今天我要记录的是一个仿excel单元格样式的datagrid样式,说到excel,用devexpress的Gridcontrol直接就可以实现,但是Datagrid也不是不可以,毕竟是原生的控件。
要实现这个给自己的需求,首先妖之道要改那些,针对datagrid,需要改的主要是以下几个Style,Datagrid本身的Style
<Style x:Key="DatagridCommon" TargetType="{x:Type DataGrid}">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="BorderBrush" Value="Gray"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
<Setter Property="ScrollViewer.PanningMode" Value="Both"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="AutoGenerateColumns" Value="False"/>
<Setter Property="ColumnHeaderHeight" Value="50"/>
<Setter Property="FontSize" Value="16"/>
<Setter Property="RowHeight" Value="40"/>
<Setter Property="HorizontalGridLinesBrush" Value="LightGray"></Setter>
<Setter Property="VerticalGridLinesBrush" Value="LightGray"></Setter>
<!--<Setter Property="AlternationCount" Value="2"/>-->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGrid}">
<Grid>
<Border Background="White" CornerRadius="6">
<Border.Effect>
<DropShadowEffect ShadowDepth="0" Direction="0" Color="#FFDADADA"/>
</Border.Effect>
</Border>
<Border