【silverlight】应用Style

cmd.Style = (Style)this.Resources[ "AlternateBigButtonStyle"];

后台设置style,来源于其他资源文件 ResourceDictionary resourceDictionary
= new ResourceDictionary(); resourceDictionary.Source = new Uri("/Styles/AlternateStyles.xaml", UriKind.Relative); Style newStyle = (Style)resourceDictionary[ "BigButtonStyle"]; cmd.Style = newStyle;


style继承:

UserControl.Resources>
 <Style x: Key="BigButtonStyle" TargetType="Button">
 <Setter Property="FontFamily" Value="Georgia" />
 <Setter Property="FontSize" Value="40" />
 <Setter Property="Padding" Value="20" />
 <Setter Property="Margin" Value="10" />
 </Style>
 <Style x: Key="EmphasizedBigButtonStyle" TargetType="Button"
 BasedOn="{StaticResource BigButtonStyle}">
 <Setter Property="BorderBrush" Value="Black" />
 <Setter Property="BorderThickness" Value="5" />
 </Style>
</UserControl.Resources>

Automatically Applying Styles by Type<UserControl.Resources>

 <Style TargetType="Button">
 <Setter Property="FontFamily" Value="Georgia" />
 <Setter Property="FontSize" Value="40" />
 <Setter Property="Foreground" Value="SlateGray" />
 <Setter Property="Padding" Value="20" />
 <Setter Property="Margin" Value="10" />
CHAPTER 14   STYLES AND BEHAVIORS
519
 </Style>
</UserControl.Resources>
<StackPanel x: Name="LayoutRoot" Background="White">
 <Button Content="A Customized Button"></Button>
 <Button Content="Another Customized Button"></Button>
</StackPanel>

//Prevent Using A style
<Button Content="A Non-Customized Button" Style="{x:Null}"></Button>

Style Binding Expressions

<UserControl.Resources>
 <FontFamily x: Key="buttonFontFamily">Georgia</FontFamily>
 <sys: Double x: Key="buttonFontSize">18</sys: Double>
 <FontWeight x: Key="buttonFontWeight">Bold</FontWeight>
 <Style x: Key="BigButtonStyle1" TargetType="Button">
 <Setter Property="FontFamily" Value="{StaticResource buttonFontFamily}" />
 <Setter Property="FontSize" Value="{StaticResource buttonFontSize}" />
 <Setter Property="FontWeight" Value="{StaticResource buttonFontWeight}" />
 </Style>

binding后台一个类设置style

public class FontInfo
{
 public FontFamily FontFamily { get; set; }
 public double FontSize { get; set; }
 public FontWeight FontWeight { get; set; }
}

And if you make that namespace available to your markup:

<UserControl xmlns: local="clr-namespace:Styles" ... >

you can use it in your resources collection and bind to it in your styles:

<UserControl.Resources>
 <local: FontInfo x: Key="buttonFont" FontFamily="Georgia" FontSize="18"
 FontWeight="Bold"></local: FontInfo>
 <Style x: Key="BigButtonStyle2" TargetType="Button">
 <Setter Property="Padding" Value="20" />
 <Setter Property="Margin" Value="10" />
 <Setter Property="FontFamily"
 Value="{Binding Source={StaticResource buttonFont}, Path=FontFamily}" />
 <Setter Property="FontSize"
 Value="{Binding Source={StaticResource buttonFont}, Path=FontSize}" />
 <Setter Property="FontWeight"
 Value="{Binding Source={StaticResource buttonFont}, Path=FontWeight}" />
 </Style>
</UserControl.Resources>

 



posted @ 2015-10-04 16:00  西楼月光  阅读(227)  评论(0编辑  收藏  举报