【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>

 



转载于:https://www.cnblogs.com/xlyg-14/p/4854652.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值