WPF引用外部样式

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

  实现方法:

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

  Code:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
                    xmlns:x=
"http://schemas.microsoft.com/winfx/2006/xaml">   
    <Style x:Key="BaseStyle" TargetType="{x:Type Control}">   
        <Setter Property="Margin" Value="5" />   
    </Style>   
    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource BaseStyle}">   
        <Setter Property="Width" Value="80" />   
        <Setter Property="Height" Value="27" />   
    </Style>   
    <Style TargetType="{x:Type GroupBox}" BasedOn="{StaticResource BaseStyle}">   
   
    </Style>   
    <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseStyle}">   
   
    </Style>   
    <Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource BaseStyle}">   
   
    </Style>   
    <Style TargetType="{x:Type ListBox}" BasedOn="{StaticResource BaseStyle}">   
   
    </Style>   
    <Style TargetType="{x:Type ProgressBar}" BasedOn="{StaticResource BaseStyle}">   
   
    </Style>   
    <Style TargetType="{x:Type TextBlock}" >   
        <Setter Property="Margin" Value="5" />   
    </Style>   
</ResourceDictionary>  

 

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

  Code:

<Window x:Class="MSSQLDocCreator.MainWindow"   
        xmlns=
"http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
        xmlns:x=
"http://schemas.microsoft.com/winfx/2006/xaml"   
        xmlns:local=
"clr-namespace:MSSQLDocCreator"   
        Title=
"MainWindow" Height="602" Width="425" WindowStartupLocation="CenterScreen">   
    <Window.Resources>   
        <ResourceDictionary x:Key="rdStyle">   
            <ResourceDictionary.MergedDictionaries>   
                <ResourceDictionary Source="Style.xaml" />   
            </ResourceDictionary.MergedDictionaries>   
        </ResourceDictionary>   
    </Window.Resources>   
    ....    
</Window>   

 

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

  Code:

<Window x:Class="MSSQLDocCreator.MainWindow"   
        xmlns=
"http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
        xmlns:x=
"http://schemas.microsoft.com/winfx/2006/xaml"   
        xmlns:local=
"clr-namespace:MSSQLDocCreator"   
        Title=
"MainWindow" Height="602" Width="425" WindowStartupLocation="CenterScreen">    
    <Window.Resources>    
    ...    
    </Window.Resources>    
    <Grid Resources="{StaticResource rdStyle}">    
    ...    
    </Grid>    
</Window>  

  完整MainWindow.xaml

  Code:

<Window x:Class="MSSQLDocCreator.MainWindow"   
        xmlns=
"http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
        xmlns:x=
"http://schemas.microsoft.com/winfx/2006/xaml"   
        xmlns:local=
"clr-namespace:MSSQLDocCreator"   
        Title=
"MainWindow" Height="602" Width="425" 
WindowStartupLocation=
"CenterScreen">   
    <Window.Resources>   
        <ObjectDataProvider x:Key="bndOutputFeilds" 
ObjectType=
"{x:Type local:AppHelper}" MethodName="GetDocFields" />   
        <ResourceDictionary x:Key="rdStyle">   
            <ResourceDictionary.MergedDictionaries>   
                <ResourceDictionary Source="Style.xaml" />   
            </ResourceDictionary.MergedDictionaries>   
        </ResourceDictionary>   
    </Window.Resources>   
    <Grid Resources="{StaticResource rdStyle}">   
        <Grid.RowDefinitions>   
            <RowDefinition Height="Auto" />   
            <RowDefinition />   
            <RowDefinition Height="Auto" />   
            <RowDefinition Height="Auto" />   
        </Grid.RowDefinitions>   
        <GroupBox Grid.Row="0" Header="MSSQL ConnectionString">   
            <StackPanel Orientation="Vertical">   
                <TextBox Name="txtConnectionString"   
                         VerticalScrollBarVisibility=
"Visible" 
TextWrapping=
"Wrap" Height="58" />   
                <StackPanel Orientation="Horizontal">   
                    <Button Name="txtTestConnect" Content="Test" />   
                    <Button Name="txtConnect" Content="Connect" />   
                </StackPanel>   
            </StackPanel>   
        </GroupBox>   
        <GroupBox Grid.Row="1" Header="Output options">   
            <Grid>   
                <Grid.RowDefinitions>   
                    <RowDefinition Height="Auto" />   
                    <RowDefinition />   
                    <RowDefinition Height="Auto" />   
                </Grid.RowDefinitions>   
                <Grid.ColumnDefinitions>   
                    <ColumnDefinition />   
                    <ColumnDefinition />   
                </Grid.ColumnDefinitions>   
                <TextBlock Grid.Row="0" Grid.Column="0" Text="Tables and Views" />   
                <TextBlock Grid.Row="0" Grid.Column="1" Text="Output fields" />   
                <ListBox Grid.Row="1" Grid.Column="0" />   
                <ListBox Name="lstFields" Grid.Row="1" Grid.Column="1"     
                         DataContext=
"{StaticResource bndOutputFeilds}"   
                         ItemsSource=
"{Binding}" IsSynchronizedWithCurrentItem="True">   
                    <ListBox.ItemTemplate>   
                        <DataTemplate>   
                            <StackPanel Orientation="Horizontal" >   
                                <CheckBox IsChecked="{Binding Path=IsSelected}" 
Click=
"chkSomeFiels_Click" />   
                                <TextBlock Text="{Binding Path=FieldName}"  
VerticalAlignment=
"Center"/>   
                            </StackPanel>   
                        </DataTemplate>   
                    </ListBox.ItemTemplate>   
                </ListBox>   
                <CheckBox Name="chkObjectsAll" IsThreeState="True" 
Grid.Row=
"2" Grid.Column="0" Content="Select All" />   
                <CheckBox Name="chkFieldsAll" IsThreeState="True" 
Grid.Row=
"2" Grid.Column="1" Content="Select All" IsChecked="True" 
Click=
"CheckBox_Click" />   
            </Grid>   
        </GroupBox>   
        <ProgressBar Height="20"  Grid.Row="2" />   
        <StackPanel  Grid.Row="3" HorizontalAlignment="Left">   
            <Button Content="Create" />   
        </StackPanel>   
    </Grid>   
</Window>   

  Code:

using System;    
using System.Collections.Generic;    
using System.Linq;    
using System.Text;    
using System.Windows;    
using System.Windows.Controls;    
using System.Windows.Data;    
using System.Windows.Documents;    
using System.Windows.Input;    
using System.Windows.Media;    
using System.Windows.Media.Imaging;    
using System.Windows.Navigation;    
using System.Windows.Shapes;    
using System.ComponentModel;    
   
namespace MSSQLDocCreator {    
    /// <summary>    
    /// MainWindow.xaml 的交互逻辑    
    /// </summary>    
    public partial class MainWindow : Window {    
        public MainWindow() {    
            InitializeComponent();    
        }    
   
        /// <summary>    
        /// Field全选    
        /// </summary>    
        private void CheckBox_Click(object sender, RoutedEventArgs e) {    
            ObjectDataProvider provider = (ObjectDataProvider)this.FindResource("bndOutputFeilds");    
            List<DocField> fields = provider.Data as List<DocField>;    
            foreach (DocField field in fields) {    
                field.IsSelected = ((CheckBox)sender).IsChecked.Value;    
            }    
        }    
   
        private void chkSomeFiels_Click(object sender, RoutedEventArgs e) {    
            CheckSelectedFields();    
        }    
   
        private void CheckSelectedFields() {    
            ObjectDataProvider provider = (ObjectDataProvider)this.FindResource("bndOutputFeilds");    
            List<DocField> fields = provider.Data as List<DocField>;    
            int checkedCount = fields.Where(c => c.IsSelected).Count();    
            if (checkedCount == 0) {    
                chkFieldsAll.IsChecked = false;    
            } else if (checkedCount == fields.Count) {    
                chkFieldsAll.IsChecked = true;    
            } else {    
                chkFieldsAll.IsChecked = null;    
            }    
        }    
   
    }    
}    

本文来自qing2005的博客,原文地址:http://blog.csdn.net/qing2005/archive/2011/04/22/6522395.aspx

 

posted @ 2012-04-12 17:05  @张凯@  阅读(287)  评论(0编辑  收藏  举报