WPF样式和资源

<Window.Resources>
  <FontFamily x:key="ButtonFontFamily">Time New Roman</FontFamily>
  <sys:Double x:key="ButtonFontSize">18</s:Double>
  <FontWeight x:key="ButtonFontWeight">Bold</FontWeight>
</Window.Resources>

1:资源里定义了三个资源,分别设置对象属性
接下来应用资源:

<Button FontFamily="{StaticResource ButtonFontFamily}"
FontWeight=="{StaticResource ButtonFontWeight}"
FontSize=="{StaticResource ButtonFontSize}"
</Button>


2:另一种方式:(WPF中的一个元素都可以使用一个样式,只能使用一个样式

创建一个独立的资源:

<Window.Resources>
  <Style x:key="BigFontButtonStyle">
    <setter Property="control.FontFamily" Value="Tinmes New     Roman"/>
    <Setter Property="control.FontSize" Value="18"/>
    <Setter Property="control.FontWeight" Value="Bold"/>
  </Style>
</Window.Resources>

应用这个独立的样式资源:

<Button Sytle="{StaticResource BigFontButtonStyle}">a Customized</Button>

Style类的属性:
Setters:设置属性值并自动关联事件处理程序的Sette对象或EventSetter对象的集合

Triggers:能够自动改变样式设置的对象集合

Resources

BasedOn:通过该属性可以创建继承其他样式设置的更复杂样式

TargetType:该属性表示应用样式的元素的类型

3:关联的事件处理程序

<Style x:key="MouseOverHighLightStyle">
  <EventSetter Event="TextBlock.MouseEnter" Handler="element_MouseEnter"/>
  <EventSetter Event=="TextBlock.MouseLeave" Handler="element_MouseLeave"/>
  <Setter Property=TextBlock.Padding" Value="5"/>
</Style>

private void element_MouseEnter(object sender,MouseEventArgs e)
{

  ((TextBlock)Senter.Background=new SolidColorBrush(Colors.LightGoldenrodYellow);

}

事件element_MouseLeave同样的道理

接下来应用给样式;

<Textklock Style="{StaticResouce MouseOverHightStyle}"> Hover Over me</Textklock >

4:简单触发器

<Style x:key="BigFontButton">
  <Style.Setters>
    <Setter Property="Control.FontFamily" Value="Time NEw Roman"/>
    <Setter Property="Control.FontSize" Value="18"/>
  </Style.Setters>
  
  <Style.Triggers>
    <Triggers Property="Control.IsFocused" Value="True">
    <Setter Property="Control.Foreground" Value="darkRed"/>
  </Style.Triggers>
</Style>
也可以定义多个触发器

 5:比较复杂的,这里用到了”行为“:

View Code
 <Style x:Key="OuterGlowStyle" TargetType="{x:Type FrameworkElement}">
            <Style.Triggers>
                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="Effect">
                        <Setter.Value>
                            <DropShadowEffect BlurRadius="10" Color="Black" Direction="0" Opacity="0.6"
 RenderingBias="Performance" ShadowDepth="0"/>
                        </Setter.Value>
                    </Setter>
                </Trigger>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Effect">
                        <Setter.Value>
                            <DropShadowEffect BlurRadius="10" Color="Black" Direction="0" Opacity="0.6"
 RenderingBias="Performance" ShadowDepth="0"/>

                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>

6:RelativeSource相对路径

    StaticSource绝对路径

    ElementSource通常绑定某个控件的Value

posted on 2012-05-30 09:08  ^-^babyGee  阅读(3089)  评论(1编辑  收藏  举报

导航