本文实例基于Silverlight实用窍门系列:71.Silverlight的Style基础之上。
一、Implicit Style(隐式样式):是不需要制定x:Key属性的Style,它的作用于是所有指定TagetType的控件,而不需要在具体的每个控件去指定其Style="{StaticResource Keyname}"。如以下代码的Style就没有指定x:Key属性,也不需要在任何ListBox控件上指定Style,会自动作用于其上:
<Style targettype="ListBox"> <Setter Property="FontSize" Value="12"></Setter> <Setter Property="Foreground" Value="Blue"></Setter> <Setter Property="FontFamily" Value="LucIDa Sans Unicode"></Setter> <Setter Property="ItemTemplate"> <Setter.Value> <DataTemplate> <StackPanel OrIEntation="Vertical" > <StackPanel OrIEntation="Horizontal" margin="5" WIDth="380"> <TextBlock Text="{Binding Artname}" margin="5 10 0 0"></TextBlock> <TextBox Text="{Binding ArtContent}" name="tbname" margin="5"></TextBox> <TextBox Text="{Binding ArtAuthor}" margin="5"></TextBox> <TextBlock Text="{Binding ArtUpdateTime}" margin="5 10 0 0"></TextBlock> </StackPanel> <StackPanel OrIEntation="Horizontal" Visibility="Collapsed"> <TextBox Text="{Binding ArtContent}" WIDth="280"></TextBox> </StackPanel> </StackPanel> </DataTemplate> </Setter.Value> </Setter> </Style>
在此例中我们将以上样式放入三个不同的ResourceDictionary资源字典文件中,并且修改其定义的样式值,然后通过后台动态加载资源字典的方式动态更换主题皮肤。具体代码如下:
ResourceDictionary redDic = new ResourceDictionary(); redDic.source = new Uri("/SLStyle;component" + Url,UriKind.relativeOrabsolute); Application.Current.Resources.MergedDictionarIEs.Clear(); Application.Current.Resources.MergedDictionarIEs.Add(redDic);
三、更换为系统皮肤
本例采用Silverlight Toolkit下面的皮肤,其路径如下:*\Silverlight\v4.0\Toolkit\Apr10\themes\Xaml\,本文选则其中三种皮肤进行加载即可,需要引入以下一些DLL和Xaml文件,然后采用自定义主题皮肤的方式进行更换。
最后我们来看其具体Xaml和Xaml.cs文件代码如下:
<GrID x:name="LayoutRoot" Background="White" DataContext="{StaticResource SourceList}"> <ListBox x:name="lbRes" ItemsSource="{Binding ArticleList}" HorizontalAlignment="left" VerticalAlignment="top" margin="0 80 0 0 " Height="400" > </ListBox> <button Content="红色主题" Height="23" HorizontalAlignment="left" margin="22,12,0" name="button1" VerticalAlignment="top" WIDth="75" Click="button1_Click" /> <button Content="蓝色主题" Height="23" HorizontalAlignment="left" margin="157,0" name="button2" VerticalAlignment="top" WIDth="75" Click="button2_Click" /> <button Content="绿色主题" Height="23" HorizontalAlignment="left" margin="284,0" name="button3" VerticalAlignment="top" WIDth="75" Click="button3_Click" /> <button Content="系统主题1" Height="23" HorizontalAlignment="left" margin="22,47,0" name="button4" VerticalAlignment="top" WIDth="75" Click="button4_Click" /> <button Content="系统主题2" Height="23" HorizontalAlignment="left" margin="157,0" name="button5" VerticalAlignment="top" WIDth="75" Click="button5_Click" /> <button Content="系统主题3" Height="23" HorizontalAlignment="left" margin="284,0" name="button6" VerticalAlignment="top" WIDth="75" Click="button6_Click" /> </GrID>
public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); ChangeStyle("/Styles/RedDic.xaml"); } private voID ChangeStyle(string Url) { ResourceDictionary redDic = new ResourceDictionary(); redDic.source = new Uri("/SLStyle;component" + Url,UriKind.relativeOrabsolute); Application.Current.Resources.MergedDictionarIEs.Clear(); Application.Current.Resources.MergedDictionarIEs.Add(redDic); } private voID button1_Click(object sender,RoutedEventArgs e) { ChangeStyle("/Styles/RedDic.xaml"); } private voID button2_Click(object sender,RoutedEventArgs e) { ChangeStyle("/Styles/BlueDic.xaml"); } private voID button3_Click(object sender,RoutedEventArgs e) { ChangeStyle("/Styles/GreenDic.xaml"); } private voID button6_Click(object sender,RoutedEventArgs e) { ChangeStyle("/Sysstyles/System.windows.Controls.Theming.Systemcolors.xaml"); } private voID button4_Click(object sender,RoutedEventArgs e) { ChangeStyle("/Sysstyles/System.windows.Controls.Theming.TwilightBlue.xaml"); } private voID button5_Click(object sender,RoutedEventArgs e) { ChangeStyle("/Sysstyles/System.windows.Controls.Theming.WhistlerBlue.xaml"); } }
效果图如下,注意在本例中没有设置主题样式的ListBox模板,大家可以自己显式设置ListBox的Itemstemplate,如需源码请点击 SLThemeStyle.zip 下载。
总结
以上是内存溢出为你收集整理的Silverlight实用窍门系列:72.Silverlight的Implicit Style、自定义主题皮肤、系统主题皮肤全部内容,希望文章能够帮你解决Silverlight实用窍门系列:72.Silverlight的Implicit Style、自定义主题皮肤、系统主题皮肤所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)