Silverlight样式定义

时间:2014-05-16 23:35:46   收藏:0   阅读:376

方法一、定义在控件内部

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

方法二、定义在控件外部

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

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

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

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

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

 

Silverlight样式定义,布布扣,bubuko.com

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