Silverlight样式定义

方法一、定义在控件内部

1 <Canvas Background="Red" Height="100" HorizontalAlignment="Left" Margin="90,80,0,0" Name="canvas1" VerticalAlignment="Top" Width="200" />

方法二、定义在控件外部

1 <UserControl.Resources>
2         <Style x:Key="StyleName" TargetType="Canvas"><!--x:Key是指样式的名称-->
3             <Setter Property="Background" Value="Red"></Setter>
4         </Style>
5     </UserControl.Resources>
6     <Grid x:Name="LayoutRoot" Background="White">
7         <Canvas Style="{StaticResource StyleName}" Height="100" HorizontalAlignment="Left" Margin="90,80,0,0" Name="canvas1" VerticalAlignment="Top" Width="200" /><!--让Canvas的样式指定为静态的资源-->        
8     </Grid>

  要想让外部样式应用于所有相同类型的控件,只要去掉资源中的样式名称,则TargetType指定的控件都将应用该样式

1 <UserControl.Resources>
2         <Style TargetType="Canvas">
3             <Setter Property="Background" Value="Red"></Setter>
4         </Style>
5     </UserControl.Resources>
6     <Grid x:Name="LayoutRoot" Background="White">
7         <Canvas Height="100" HorizontalAlignment="Left" Margin="90,80,0,0" Name="canvas1" VerticalAlignment="Top" Width="200" />
8         <Canvas Height="100" HorizontalAlignment="Left" Margin="121,188,0,0" Name="canvas2" VerticalAlignment="Top" Width="200" />
9     </Grid>

 方法三、定义全局的样式
  将样式放到App.xaml文件的资源定义里

1  <Application.Resources>
2         <Style TargetType="Canvas">           
3             <Setter Property="Background" Value="Red"></Setter>
4         </Style>
5     </Application.Resources>

 

你可能感兴趣的:(silverlight)