现在的位置: 首页 > 综合 > 正文

WPF引用外部样式

2014年03月15日 ⁄ 综合 ⁄ 共 7826字 ⁄ 字号 评论关闭

假设一个应用程序中,某个窗口需要使用样式,但是样式非常多,写在一个窗口中代码分类不方便。最好Style写在专门的xaml文件中,然后引用到窗口中,就像HTML引用外部css文件一样。

实现方法:
1.创建新建项“添加/资源字典”Style.xaml,并添加Style样式

Code:
  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  2.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">  
  3.     <Style x:Key="BaseStyle" TargetType="{x:Type Control}">  
  4.         <Setter Property="Margin" Value="5" />  
  5.     </Style>  
  6.     <Style TargetType="{x:Type Button}" BasedOn="{StaticResource BaseStyle}">  
  7.         <Setter Property="Width" Value="80" />  
  8.         <Setter Property="Height" Value="27" />  
  9.     </Style>  
  10.     <Style TargetType="{x:Type GroupBox}" BasedOn="{StaticResource BaseStyle}">  
  11.   
  12.     </Style>  
  13.     <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseStyle}">  
  14.   
  15.     </Style>  
  16.     <Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource BaseStyle}">  
  17.   
  18.     </Style>  
  19.     <Style TargetType="{x:Type ListBox}" BasedOn="{StaticResource BaseStyle}">  
  20.   
  21.     </Style>  
  22.     <Style TargetType="{x:Type ProgressBar}" BasedOn="{StaticResource BaseStyle}">  
  23.   
  24.     </Style>  
  25.     <Style TargetType="{x:Type TextBlock}" >  
  26.         <Setter Property="Margin" Value="5" />  
  27.     </Style>  
  28. </ResourceDictionary>  

2.在窗口中引用外部资源,注意:在Window中添加外部样式需要指定key,而Application中则不需要,所以这里rdStyle就是引用外部样式Style.xaml的key

Code:
  1. <Window x:Class="MSSQLDocCreator.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:local="clr-namespace:MSSQLDocCreator"  
  5.         Title="MainWindow" Height="602" Width="425" WindowStartupLocation="CenterScreen">  
  6.     <Window.Resources>  
  7.         <ResourceDictionary x:Key="rdStyle">  
  8.             <ResourceDictionary.MergedDictionaries>  
  9.                 <ResourceDictionary Source="Style.xaml" />  
  10.             </ResourceDictionary.MergedDictionaries>  
  11.         </ResourceDictionary>  
  12.     </Window.Resources>  
  13.     ....   
  14. </Window>  

3.将样式应用到窗口的布局上

Code:
  1. <Window x:Class="MSSQLDocCreator.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:local="clr-namespace:MSSQLDocCreator"  
  5.         Title="MainWindow" Height="602" Width="425" WindowStartupLocation="CenterScreen">   
  6.     <Window.Resources>   
  7.     ...   
  8.     </Window.Resources>   
  9.     <Grid Resources="{StaticResource rdStyle}">   
  10.     ...   
  11.     </Grid>   
  12. </Window>  

见效果图:

完整MainWindow.xaml

Code:
  1. <Window x:Class="MSSQLDocCreator.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:local="clr-namespace:MSSQLDocCreator"  
  5.         Title="MainWindow" Height="602" Width="425" WindowStartupLocation="CenterScreen">  
  6.     <Window.Resources>  
  7.         <ObjectDataProvider x:Key="bndOutputFeilds" ObjectType="{x:Type local:AppHelper}" MethodName="GetDocFields" />  
  8.         <ResourceDictionary x:Key="rdStyle">  
  9.             <ResourceDictionary.MergedDictionaries>  
  10.                 <ResourceDictionary Source="Style.xaml" />  
  11.             </ResourceDictionary.MergedDictionaries>  
  12.         </ResourceDictionary>  
  13.     </Window.Resources>  
  14.     <Grid Resources="{StaticResource rdStyle}">  
  15.         <Grid.RowDefinitions>  
  16.             <RowDefinition Height="Auto" />  
  17.             <RowDefinition />  
  18.             <RowDefinition Height="Auto" />  
  19.             <RowDefinition Height="Auto" />  
  20.         </Grid.RowDefinitions>  
  21.         <GroupBox Grid.Row="0" Header="MSSQL ConnectionString">  
  22.             <StackPanel Orientation="Vertical">  
  23.                 <TextBox Name="txtConnectionString"  
  24.                          VerticalScrollBarVisibility="Visible" TextWrapping="Wrap" Height="58" />  
  25.                 <StackPanel Orientation="Horizontal">  
  26.                     <Button Name="txtTestConnect" Content="Test" />  
  27.                     <Button Name="txtConnect" Content="Connect" />  
  28.                 </StackPanel>  
  29.             </StackPanel>  
  30.         </GroupBox>  
  31.         <GroupBox Grid.Row="1" Header="Output options">  
  32.             <Grid>  
  33.                 <Grid.RowDefinitions>  
  34.                     <RowDefinition Height="Auto" />  
  35.                     <RowDefinition />  
  36.                     <RowDefinition Height="Auto" />  
  37.                 </Grid.RowDefinitions>  
  38.                 <Grid.ColumnDefinitions>  
  39.                     <ColumnDefinition />  
  40.                     <ColumnDefinition />  
  41.                 </Grid.ColumnDefinitions>  
  42.                 <TextBlock Grid.Row="0" Grid.Column="0" Text="Tables and Views" />  
  43.                 <TextBlock Grid.Row="0" Grid.Column="1" Text="Output fields" />  
  44.                 <ListBox Grid.Row="1" Grid.Column="0" />  
  45.                 <ListBox Name="lstFields" Grid.Row="1" Grid.Column="1"    
  46.                          DataContext="{StaticResource bndOutputFeilds}"  
  47.                          ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True">  
  48.                     <ListBox.ItemTemplate>  
  49.                         <DataTemplate>  
  50.                             <StackPanel Orientation="Horizontal" >  
  51.                                 <CheckBox IsChecked="{Binding Path=IsSelected}" Click="chkSomeFiels_Click" />  
  52.                                 <TextBlock Text="{Binding Path=FieldName}"  VerticalAlignment="Center"/>  
  53.                             </StackPanel>  
  54.                         </DataTemplate>  
  55.                     </ListBox.ItemTemplate>  
  56.                 </ListBox>  
  57.                 <CheckBox Name="chkObjectsAll" IsThreeState="True" Grid.Row="2" Grid.Column="0" Content="Select All" />  
  58.                 <CheckBox Name="chkFieldsAll" IsThreeState="True" Grid.Row="2" Grid.Column="1" Content="Select All" IsChecked="True" Click="CheckBox_Click" />  
  59.             </Grid>  
  60.         </GroupBox>  
  61.         <ProgressBar Height="20"  Grid.Row="2" />  
  62.         <StackPanel  Grid.Row="3" HorizontalAlignment="Left">  
  63.             <Button Content="Create" />  
  64.         </StackPanel>  
  65.     </Grid>  
  66. </Window>  
Code:
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4. using System.Text;   
  5. using System.Windows;   
  6. using System.Windows.Controls;   
  7. using System.Windows.Data;   
  8. using System.Windows.Documents;   
  9. using System.Windows.Input;   
  10. using System.Windows.Media;   
  11. using System.Windows.Media.Imaging;   
  12. using System.Windows.Navigation;   
  13. using System.Windows.Shapes;   
  14. using System.ComponentModel;   
  15.   
  16. namespace MSSQLDocCreator {   
  17.     /// <summary>   
  18.     /// MainWindow.xaml 的交互逻辑   
  19.     /// </summary>   
  20.     public partial class MainWindow : Window {   
  21.         public MainWindow() {   
  22.             InitializeComponent();   
  23.         }   
  24.   
  25.         /// <summary>   
  26.         /// Field全选   
  27.         /// </summary>   
  28.         private void CheckBox_Click(object sender, RoutedEventArgs e) {   
  29.             ObjectDataProvider provider = (ObjectDataProvider)this.FindResource("bndOutputFeilds");   
  30.             List<DocField> fields = provider.Data as List<DocField>;   
  31.             foreach (DocField field in fields) {   
  32.                 field.IsSelected = ((CheckBox)sender).IsChecked.Value;   
  33.             }   
  34.         }   
  35.   
  36.         private void chkSomeFiels_Click(object sender, RoutedEventArgs e) {   
  37.             CheckSelectedFields();   
  38.         }   
  39.   
  40.         private void CheckSelectedFields() {   
  41.             ObjectDataProvider provider = (ObjectDataProvider)this.FindResource("bndOutputFeilds");   
  42.             List<DocField> fields = provider.Data as List<DocField>;   
  43.             int checkedCount = fields.Where(c => c.IsSelected).Count();   
  44.             if (checkedCount == 0) {   
  45.                 chkFieldsAll.IsChecked = false;   
  46.             } else if (checkedCount == fields.Count) {   
  47.                 chkFieldsAll.IsChecked = true;   
  48.             } else {   
  49.                 chkFieldsAll.IsChecked = null;   
  50.             }   
  51.         }   
  52.   
  53.     }   
  54. }   

 

抱歉!评论已关闭.