假设一个应用程序中,某个窗口需要使用样式,但是样式非常多,写在一个窗口中代码分类不方便。最好Style写在专门的xaml文件中,然后引用到窗口中,就像HTML引用外部css文件一样。
实现方法:
1.创建新建项“添加/资源字典”Style.xaml,并添加Style样式
- <ResourceDictionaryxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
- <Stylex:Key="BaseStyle"TargetType="{x:TypeControl}">
- <SetterProperty="Margin"Value="5"/>
- </Style>
- <StyleTargetType="{x:TypeButton}"BasedOn="{StaticResourceBaseStyle}">
- <SetterProperty="Width"Value="80"/>
- <SetterProperty="Height"Value="27"/>
- </Style>
- <StyleTargetType="{x:TypeGroupBox}"BasedOn="{StaticResourceBaseStyle}">
- </Style>
- <StyleTargetType="{x:TypeTextBox}"BasedOn="{StaticResourceBaseStyle}">
- </Style>
- <StyleTargetType="{x:TypeCheckBox}"BasedOn="{StaticResourceBaseStyle}">
- </Style>
- <StyleTargetType="{x:TypeListBox}"BasedOn="{StaticResourceBaseStyle}">
- </Style>
- <StyleTargetType="{x:TypeProgressBar}"BasedOn="{StaticResourceBaseStyle}">
- </Style>
- <StyleTargetType="{x:TypeTextBlock}">
- <SetterProperty="Margin"Value="5"/>
- </Style>
- </ResourceDictionary>
2.在窗口中引用外部资源,注意:在Window中添加外部样式需要指定key,而Application中则不需要,所以这里rdStyle就是引用外部样式Style.xaml的key
- <Windowx: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>
- <ResourceDictionaryx:Key="rdStyle">
- <ResourceDictionary.MergedDictionaries>
- <ResourceDictionarySource="Style.xaml"/>
- </ResourceDictionary.MergedDictionaries>
- </ResourceDictionary>
- </Window.Resources>
- ....
- </Window>
3.将样式应用到窗口的布局上
- <Windowx: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>
- <GridResources="{StaticResourcerdStyle}">
- ...
- </Grid>
- </Window>
见效果图:
完整MainWindow.xaml
- <Windowx: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>
- <ObjectDataProviderx:Key="bndOutputFeilds"ObjectType="{x:Typelocal:AppHelper}"MethodName="GetDocFields"/>
- <ResourceDictionaryx:Key="rdStyle">
- <ResourceDictionary.MergedDictionaries>
- <ResourceDictionarySource="Style.xaml"/>
- </ResourceDictionary.MergedDictionaries>
- </ResourceDictionary>
- </Window.Resources>
- <GridResources="{StaticResourcerdStyle}">
- <Grid.RowDefinitions>
- <RowDefinitionHeight="Auto"/>
- <RowDefinition/>
- <RowDefinitionHeight="Auto"/>
- <RowDefinitionHeight="Auto"/>
- </Grid.RowDefinitions>
- <GroupBoxGrid.Row="0"Header="MSSQLConnectionString">
- <StackPanelOrientation="Vertical">
- <TextBoxName="txtConnectionString"
- VerticalScrollBarVisibility="Visible"TextWrapping="Wrap"Height="58"/>
- <StackPanelOrientation="Horizontal">
- <ButtonName="txtTestConnect"Content="Test"/>
- <ButtonName="txtConnect"Content="Connect"/>
- </StackPanel>
- </StackPanel>
- </GroupBox>
- <GroupBoxGrid.Row="1"Header="Outputoptions">
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinitionHeight="Auto"/>
- <RowDefinition/>
- <RowDefinitionHeight="Auto"/>
- </Grid.RowDefinitions>
- <Grid.ColumnDefinitions>
- <ColumnDefinition/>
- <ColumnDefinition/>
- </Grid.ColumnDefinitions>
- <TextBlockGrid.Row="0"Grid.Column="0"Text="TablesandViews"/>
- <TextBlockGrid.Row="0"Grid.Column="1"Text="Outputfields"/>
- <ListBoxGrid.Row="1"Grid.Column="0"/>
- <ListBoxName="lstFields"Grid.Row="1"Grid.Column="1"
- DataContext="{StaticResourcebndOutputFeilds}"
- ItemsSource="{Binding}"IsSynchronizedWithCurrentItem="True">
- <ListBox.ItemTemplate>
- <DataTemplate>
- <StackPanelOrientation="Horizontal">
- <CheckBoxIsChecked="{BindingPath=IsSelected}"Click="chkSomeFiels_Click"/>
- <TextBlockText="{BindingPath=FieldName}"VerticalAlignment="Center"/>
- </StackPanel>
- </DataTemplate>
- </ListBox.ItemTemplate>
- </ListBox>
- <CheckBoxName="chkObjectsAll"IsThreeState="True"Grid.Row="2"Grid.Column="0"Content="SelectAll"/>
- <CheckBoxName="chkFieldsAll"IsThreeState="True"Grid.Row="2"Grid.Column="1"Content="SelectAll"IsChecked="True"Click="CheckBox_Click"/>
- </Grid>
- </GroupBox>
- <ProgressBarHeight="20"Grid.Row="2"/>
- <StackPanelGrid.Row="3"HorizontalAlignment="Left">
- <ButtonContent="Create"/>
- </StackPanel>
- </Grid>
- </Window>
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingSystem.Windows;
- usingSystem.Windows.Controls;
- usingSystem.Windows.Data;
- usingSystem.Windows.Documents;
- usingSystem.Windows.Input;
- usingSystem.Windows.Media;
- usingSystem.Windows.Media.Imaging;
- usingSystem.Windows.Navigation;
- usingSystem.Windows.Shapes;
- usingSystem.ComponentModel;
- namespaceMSSQLDocCreator{
- ///<summary>
- ///MainWindow.xaml的交互逻辑
- ///</summary>
- publicpartialclassMainWindow:Window{
- publicMainWindow(){
- InitializeComponent();
- }
- ///<summary>
- ///Field全选
- ///</summary>
- privatevoidCheckBox_Click(objectsender,RoutedEventArgse){
- ObjectDataProviderprovider=(ObjectDataProvider)this.FindResource("bndOutputFeilds");
- List<DocField>fields=provider.DataasList<DocField>;
- foreach(DocFieldfieldinfields){
- field.IsSelected=((CheckBox)sender).IsChecked.Value;
- }
- }
- privatevoidchkSomeFiels_Click(objectsender,RoutedEventArgse){
- CheckSelectedFields();
- }
- privatevoidCheckSelectedFields(){
- ObjectDataProviderprovider=(ObjectDataProvider)this.FindResource("bndOutputFeilds");
- List<DocField>fields=provider.DataasList<DocField>;
- intcheckedCount=fields.Where(c=>c.IsSelected).Count();
- if(checkedCount==0){
- chkFieldsAll.IsChecked=false;
- }elseif(checkedCount==fields.Count){
- chkFieldsAll.IsChecked=true;
- }else{
- chkFieldsAll.IsChecked=null;
- }
- }
- }
- }