SilverLight样式表继承

SilverLight样式表继承与Css样式表继承很相似,只不过是语法结构不同而已。SilverLight功能更强大。

还是举最简单的例子,开启这扇大门:

新建文件:

\Style\Main.xaml:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation">
    <Style x:Key="button1" TargetType="Button">
        <Setter Property="Background" Value="Black"/>
        <Setter Property="Foreground" Value="Blue"/>
        <Setter Property="Margin" Value="20"/>
    </Style>
</ResourceDictionary>

 

APP.xaml:

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>

 

      <!--引用外部样式表-->
                <ResourceDictionary Source="Style/Main.xaml"></ResourceDictionary>
                </ResourceDictionary.MergedDictionaries>

 

                 <!--在这里也可定义其它样式表例如-->

                  <Style x:Key="button2" TargetType="Button"  BasedOn="{StaticResource button1}">
              <Setter Property="Foreground" Value="Red"/>
        </Style>

        </ResourceDictionary>
</Application.Resources>

 

MainPage.xaml

 <UserControl.Resources>
        <Style x:Key="button3" TargetType="Button" BasedOn="{StaticResource button2}">
            <Setter Property="FontSize" Value="99"/>
        </Style>

 </UserControl.Resources>

 

<Button Content="Button" Style="{StaticResource button3}"  HorizontalAlignment="Left" VerticalAlignment="Top" />

 

 

运行结果可以看到所有样式都加载到Button中。OK!

继承顺序:

Main.xaml--APP.xaml--MainPage.xaml

 

posted on 2010-07-03 10:48  松原蔡晓冬  阅读(1740)  评论(2)    收藏  举报

导航