WPF开发总结

来源:互联网 发布:电梯乘坐10人答案 知乎 编辑:程序博客网 时间:2024/06/05 05:49

一、组成部分:


样式(Style):

定义:

<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">    <!--Window窗体-->    <SolidColorBrush x:Key="WindowBackground" Color="#007ACB"></SolidColorBrush>     <FontFamily x:Key="FontFamily" >Microsoft YaHei</FontFamily>    <sys:Double x:Key="FontSize">13</sys:Double>    <sys:Double x:Key="DisableOpacity">0.5</sys:Double>    <sys:Double x:Key="ReadonlyOpacity">0.88</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></ResourceDictionary>

添加引用:

    <Application.Resources>             <ResourceDictionary>            <Style TargetType="{x:Type ScrollBar}" BasedOn="{StaticResource DefaultScrollBar}"></Style>            <Style TargetType="{x:Type ScrollViewer}" BasedOn="{StaticResource DefaultScrollViewer}"></Style>            <ResourceDictionary.MergedDictionaries>                <ResourceDictionary Source="pack://application:,,,/HebianGu.Product.WinHelper;component/Style/Control/FIcon.xaml" />                             <ResourceDictionary Source=".\Style\Brush\Colors.xaml"/>            </ResourceDictionary.MergedDictionaries>    </ResourceDictionary>             </Application.Resources>

<ResourceDictionary Source=".\Style\Brush\Colors.xaml"/>


使用:

    <!--ToolTip-->    <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>

 <Setter Property="Background" Value="{StaticResource HeaderBackground}"/>


模板(Template):

定义并设置模板:

<Style x:Key="MyButton" TargetType="{x:Type Button}"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type Button}"><Grid><Rectangle x:Name="bg" Stroke="Black" StrokeThickness="0" Visibility="Hidden"><Rectangle.Fill><ImageBrush ImageSource="skin/Button/btbg.png" Stretch="Uniform"/></Rectangle.Fill></Rectangle><Rectangle x:Name="fg" Stroke="Black" StrokeThickness="0" Visibility="Hidden" RadiusX="0" RadiusY="0"><Rectangle.Fill><ImageBrush ImageSource="skin/Button/btfg.png" Stretch="Uniform"/></Rectangle.Fill></Rectangle><TextBlock x:Name="textBlock" HorizontalAlignment="Center" Margin="0" TextWrapping="Wrap" Text="{TemplateBinding Content}" d:LayoutOverrides="Height" VerticalAlignment="Center" Foreground="#FF020F5C"/></Grid></ControlTemplate></Setter.Value></Setter></Style>

设置模板节点:<Setter Property="Template"> 

模板定义:<ControlTemplate TargetType="{x:Type Button}">

单独定义的模板:


<ControlTemplate x:Key="MyButtonTemplate" TargetType="{x:Type Button}"><Grid><Rectangle x:Name="bg" Stroke="Black" StrokeThickness="0" Visibility="Hidden"><Rectangle.Fill><ImageBrush ImageSource="skin/Button/btbg.png" Stretch="Uniform"/></Rectangle.Fill></Rectangle><Rectangle x:Name="fg" Stroke="Black" StrokeThickness="0" Visibility="Hidden" RadiusX="0" RadiusY="0"><Rectangle.Fill><ImageBrush ImageSource="skin/Button/btfg.png" Stretch="Uniform"/></Rectangle.Fill></Rectangle><TextBlock x:Name="textBlock" HorizontalAlignment="Center" Margin="0" TextWrapping="Wrap" Text="{TemplateBinding Content}" d:LayoutOverrides="Height" VerticalAlignment="Center" Foreground="#FF020F5C"/></Grid></ControlTemplate>

使用单独定义的模板:
      <Button Content="测试按钮" Template="{StaticResource MyButtonTemplate}" ></Button>


触发器(Trigger)

示例:
<ControlTemplate.Triggers><Trigger Property="IsFocused" Value="True"/><Trigger Property="IsDefaulted" Value="True"/><Trigger Property="IsMouseOver" Value="True"><Setter Property="Background" TargetName="border"><Setter.Value><LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"><GradientStop Color="#87A58686" Offset="0"/><GradientStop Color="#FF702323" Offset="0.047"/><GradientStop Color="#01FFFFFF" Offset="0.884"/><GradientStop Color="#46FCFAFA" Offset="1"/><GradientStop Color="#33905454" Offset="0.862"/></LinearGradientBrush></Setter.Value></Setter></Trigger><Trigger Property="IsPressed" Value="True"><Setter Property="Background" TargetName="border"><Setter.Value><LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"><GradientStop Color="#44FFFFFF" Offset="0"/><GradientStop Offset="1" Color="#A8831111"/><GradientStop Color="#7A41697A" Offset="0.052"/></LinearGradientBrush></Setter.Value></Setter></Trigger><Trigger Property="IsEnabled" Value="False"/></ControlTemplate.Triggers>

绑定属性(Property):


绑定命令(Command):


动画:


类型转换(Converter):


资源:


自定义元素:


0 0