样式和资源

Setter元素定义Property和Value属性,并 给指定的属性设置一个值。 这里设置 Background、 FontSize 和 FontWeight属性。 把Style设置为 TargetType Button,以 便可以 直接访问Button的 属性。 如果没有设置样式的TargetType,就可以通过 Button.Background、 Button.FontSize  访问 属性。 如果需要设置不同 元素类型的 属性,这就很重要。(以下示例中TargetType是固定的类型写其它的会提示错误,对样式的共享没什么帮助!)

指定 x:Type标记扩展{x:Type Button},也可以将样式赋予按钮(一个类型)。 要定义需要引用的样式,必须设置x:Key.  对于层次结构,需要注意是否为样式使用了没有Key的TargetType,如果没有Key会应用于某一类型,有Key则需要代码引用。

    <Grid>
        <Grid.Style>
            <Style TargetType="Grid">
                <Setter Property="Background" Value="Yellow"/>
            </Style>
        </Grid.Style>
</Grid>

样式可以放在资源中(并且样式通常放在资源中):

   <Window.Resources>
        <Style x:Key="AnimatedButtonStyle" TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid>
                            <Rectangle Name="outline" RadiusX="9" RadiusY="9"
                                       Stroke="Black" Fill="{TemplateBinding Background}"
                                       StrokeThickness="1.6">
                                
                            </Rectangle>
                            <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Trigger.EnterActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <DoubleAnimation Duration="0:0:0.3"
                                                             AutoReverse="True"
                                                             Storyboard.TargetProperty="(Rectangle.StrokeThickness)"
                                                             Storyboard.TargetName="outline"
                                                             By="1.2"/>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </Trigger.EnterActions>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

在资源中定义无素:

<StackPanel x:Name="myContainer">
        <StackPanel.Resources>
            <LinearGradientBrush x:Key="myGradientBrush" StartPoint="0,0" EndPoint="0.3,1">
                <GradientStop Offset="0.0" Color="LightCoral"/>
                <GradientStop Offset="0.14" Color="Cyan"/>
                <GradientStop Offset="0.7" Color="DarkCyan"/>
            </LinearGradientBrush>
        </StackPanel.Resources>
        <Button Width="200" Height="50" Margin="5" Style="{StaticResource RoundedGelButton}"  Content="Click Me" Click="Button_Click">
        </Button>
        <TextBox />
        <ListBox x:Name="list1"/>
    </StackPanel>

 

资源按层次结构来搜索。 如果用Window元 素定义资源,它 就会应用于Window的所有子元素。如果Window包含一个Grid讯 ,且 该Grid包含一个StackPanel,同 时如果资源是用 StackPanel定义的,该资源就会应用于StackPanel中的所有控件 。如果StackPanel包含一个按钮,但只用该按钮定义资源,这个样式就只对该按钮有效。

系统资源:

还有许多系统范围内的颜色和字体资源,它 们可用于所有应用程序。 这些资源用 SystemColors、SystemFonts和SystemParameters类定义.

动态资源:

通过StaticResource标记扩展,在加载期间搜索资源。如果在运行程序的过程中改变了资源,就应使用DynamicRcsource标记扩展。

将样式应用多个窗体及资源字典的引用:

如果需要将同 一个样式应用于多个窗口 ,就 可以 用应用程序定义样式。 在一个Visual Studio WPF项目中,创建 App.xaml文件,以定义应用程序的全局资源。 应用程序样式对其中的每个窗口都有效;每个元素都可以访问用应用程序定义的资源。 如果通过父窗口找不到资源,就 可以通过Application继续搜索资源。

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary1.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

 

 

posted @ 2014-05-28 10:54  高_山_流_水  阅读(236)  评论(0)    收藏  举报