WPF之自定义控件

1.先定义画刷,一般存为资源字典

格式:

  <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:sys="clr-namespace:System;assembly=mscorlib"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <!--定义 画刷-->
   <!-- 字体-->
   <!-- 等 -->
  </ResourceDictionary>

定义SolidBrush:

 <SolidColorBrush x:Key="WindowBackground" Color="#007ACB" />
 <SolidColorBrush x:Key="WindowBorderBrush" Color="Transparent" />
 <SolidColorBrush x:key="WindowForeground" Color="White" />

定义LinearGradientBrush:

 <LinearGradientBrush x:Key="CaptionBackground" StartPoint="0.5,0" EndPoint="0.5,1">
 <GradientStop Color="#571457" Offset="0"/>
 <GradientStop Color="#6A196A" Offset="1"/>
 </LinearGradientBrush>

设置边框效果:

 1<DropShadowEffect x:Key="DefaultDropShadow" Color="Black" 
BlurRadius="5" ShadowDeph="2" Direction="31" Opacity="0.6"/>

定义字体:

 <FontFamily x:Key="FontFamily">Microsoft YaHei</FontFamily>

定义数值和字符串:

  <sys:Double x:Key="FontSize">13</sys:Double>
  <sys:Double x:Key="WatermarkOpacity">0.4</sys:Double>
  <sys:String x:Key="DateFormat">yyyy年MM月dd日</sys:String>
  <sys:String x:Key="DateTimeFormat">yyyy/MM/dd HH:mm:ss</sys:String>

2.定义样式:

格式:注意需要引用当前类库的命名空间

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:local="clr-namespace:SLT.Controls"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <!---->
  <!--样式-->
  <!---->
 </ResourceDictionary>

TargetType指定样式的应用对象

 <Style TargetType="{x:Type TextBlock}">
 <Setter Property="Foreground" Value="{StaticResource TextForeground}"/>
 <Setter Property="FontFamily" Value="{StaticResource FontFamily}"/>
 <Setter Property="FontSize" Value="{StaticResource FontSize}"/>
 </Style>

带ControlTemplate的样式:

 <Style TargetType="{x:Type ToolTip}">
 <Setter Property="Foreground" Value="{StaticResource TextForeground}"/>
 <Setter Property="FontFamily" Value="{StaticResource FontFamily}"/>
 <Setter Property="FontSize" Value="{StaticResource FontSize}"/>
 <Setter Property="Background" Value="{StaticResource HeaderBackground}"/>
 <Setter Property="BorderBrush" Value="{StaticResource FocusBorderBrush}"/>
 <Setter Property="BorderThickness" Value="1"/>
 <Setter Property="Template">
 <Setter.Value>
   <ControlTemplate TargetType="{x:Type ToolTip}">
      <Border CornerRadius="2" BorderThickness="{TemplateBinding BorderThickness}" 
BorderBrush="{TemplateBinding BorderBrush}"     Background="{TemplateBinding Background}">       <ContentPresenter Margin="8,5,8,5"/>   </Border> </ControlTemplate> </Setter.Value> </Setter> </Style>

 3.添加MergedDictionaries:

所有的样式文件需要添加说明,格式如下:

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
     <ResourceDictionary.MergedDictionaries>
         <!---->
         <!--此处添加样式文件-->
         <!---->
     </ResourceDictionary.MergedDictionaries>
 </ResourceDictionary>

样式文件说明需要说清文件的具体位置,如下:

1  <ResourceDictionary Source="pack://application:,,,/SLT.Controls;component/Control/HighTextBlock.xaml" />
上一篇:luogu3379 【模板】最近公共祖先(LCA) 倍增法


下一篇:获取本机IP、mac地址、计算机名