WPF样式和资源2

时间:2014-05-09 03:56:46   收藏:0   阅读:266
bubuko.com,布布扣
<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>
bubuko.com,布布扣

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

bubuko.com,布布扣
<Button FontFamily="{StaticResource ButtonFontFamily}"
FontWeight=="{StaticResource ButtonFontWeight}"
FontSize=="{StaticResource ButtonFontSize}"
</Button>
bubuko.com,布布扣

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

创建一个独立的资源:

 

bubuko.com,布布扣
<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>
bubuko.com,布布扣

应用这个独立的样式资源

bubuko.com,布布扣
<Button Sytle="{StaticResource BigFontButtonStyle}">a Customized</Button>
bubuko.com,布布扣

 

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

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

Resources

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

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

 

3:关联的事件处理程序

bubuko.com,布布扣
<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>
bubuko.com,布布扣
bubuko.com,布布扣
private void element_MouseEnter(object sender,MouseEventArgs e)
{

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

}
bubuko.com,布布扣

事件element_MouseLeave同样的道理

接下来应用给样式;

bubuko.com,布布扣
<Textklock Style="{StaticResouce MouseOverHightStyle}"> Hover Over me</Textklock >
bubuko.com,布布扣

4:简单触发器

bubuko.com,布布扣
<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>
也可以定义多个触发器
bubuko.com,布布扣

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

bubuko.com,布布扣
<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>
bubuko.com,布布扣

 

6:RelativeSource相对路径

    StaticSource绝对路径

    ElementSource通常绑定某个控件的Value

WPF样式和资源2,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!