WPF引用外部样式

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

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

Code:
  1. <ResourceDictionaryxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  3. <Stylex:Key="BaseStyle"TargetType="{x:TypeControl}">
  4. <SetterProperty="Margin"Value="5"/>
  5. </Style>
  6. <StyleTargetType="{x:TypeButton}"BasedOn="{StaticResourceBaseStyle}">
  7. <SetterProperty="Width"Value="80"/>
  8. <SetterProperty="Height"Value="27"/>
  9. </Style>
  10. <StyleTargetType="{x:TypeGroupBox}"BasedOn="{StaticResourceBaseStyle}">
  11. </Style>
  12. <StyleTargetType="{x:TypeTextBox}"BasedOn="{StaticResourceBaseStyle}">
  13. </Style>
  14. <StyleTargetType="{x:TypeCheckBox}"BasedOn="{StaticResourceBaseStyle}">
  15. </Style>
  16. <StyleTargetType="{x:TypeListBox}"BasedOn="{StaticResourceBaseStyle}">
  17. </Style>
  18. <StyleTargetType="{x:TypeProgressBar}"BasedOn="{StaticResourceBaseStyle}">
  19. </Style>
  20. <StyleTargetType="{x:TypeTextBlock}">
  21. <SetterProperty="Margin"Value="5"/>
  22. </Style>
  23. </ResourceDictionary>

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

Code:
  1. <Windowx: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. <ResourceDictionaryx:Key="rdStyle">
  8. <ResourceDictionary.MergedDictionaries>
  9. <ResourceDictionarySource="Style.xaml"/>
  10. </ResourceDictionary.MergedDictionaries>
  11. </ResourceDictionary>
  12. </Window.Resources>
  13. ....
  14. </Window>

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

Code:
  1. <Windowx: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. <GridResources="{StaticResourcerdStyle}">
  10. ...
  11. </Grid>
  12. </Window>

见效果图:

完整MainWindow.xaml

Code:
  1. <Windowx: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. <ObjectDataProviderx:Key="bndOutputFeilds"ObjectType="{x:Typelocal:AppHelper}"MethodName="GetDocFields"/>
  8. <ResourceDictionaryx:Key="rdStyle">
  9. <ResourceDictionary.MergedDictionaries>
  10. <ResourceDictionarySource="Style.xaml"/>
  11. </ResourceDictionary.MergedDictionaries>
  12. </ResourceDictionary>
  13. </Window.Resources>
  14. <GridResources="{StaticResourcerdStyle}">
  15. <Grid.RowDefinitions>
  16. <RowDefinitionHeight="Auto"/>
  17. <RowDefinition/>
  18. <RowDefinitionHeight="Auto"/>
  19. <RowDefinitionHeight="Auto"/>
  20. </Grid.RowDefinitions>
  21. <GroupBoxGrid.Row="0"Header="MSSQLConnectionString">
  22. <StackPanelOrientation="Vertical">
  23. <TextBoxName="txtConnectionString"
  24. VerticalScrollBarVisibility="Visible"TextWrapping="Wrap"Height="58"/>
  25. <StackPanelOrientation="Horizontal">
  26. <ButtonName="txtTestConnect"Content="Test"/>
  27. <ButtonName="txtConnect"Content="Connect"/>
  28. </StackPanel>
  29. </StackPanel>
  30. </GroupBox>
  31. <GroupBoxGrid.Row="1"Header="Outputoptions">
  32. <Grid>
  33. <Grid.RowDefinitions>
  34. <RowDefinitionHeight="Auto"/>
  35. <RowDefinition/>
  36. <RowDefinitionHeight="Auto"/>
  37. </Grid.RowDefinitions>
  38. <Grid.ColumnDefinitions>
  39. <ColumnDefinition/>
  40. <ColumnDefinition/>
  41. </Grid.ColumnDefinitions>
  42. <TextBlockGrid.Row="0"Grid.Column="0"Text="TablesandViews"/>
  43. <TextBlockGrid.Row="0"Grid.Column="1"Text="Outputfields"/>
  44. <ListBoxGrid.Row="1"Grid.Column="0"/>
  45. <ListBoxName="lstFields"Grid.Row="1"Grid.Column="1"
  46. DataContext="{StaticResourcebndOutputFeilds}"
  47. ItemsSource="{Binding}"IsSynchronizedWithCurrentItem="True">
  48. <ListBox.ItemTemplate>
  49. <DataTemplate>
  50. <StackPanelOrientation="Horizontal">
  51. <CheckBoxIsChecked="{BindingPath=IsSelected}"Click="chkSomeFiels_Click"/>
  52. <TextBlockText="{BindingPath=FieldName}"VerticalAlignment="Center"/>
  53. </StackPanel>
  54. </DataTemplate>
  55. </ListBox.ItemTemplate>
  56. </ListBox>
  57. <CheckBoxName="chkObjectsAll"IsThreeState="True"Grid.Row="2"Grid.Column="0"Content="SelectAll"/>
  58. <CheckBoxName="chkFieldsAll"IsThreeState="True"Grid.Row="2"Grid.Column="1"Content="SelectAll"IsChecked="True"Click="CheckBox_Click"/>
  59. </Grid>
  60. </GroupBox>
  61. <ProgressBarHeight="20"Grid.Row="2"/>
  62. <StackPanelGrid.Row="3"HorizontalAlignment="Left">
  63. <ButtonContent="Create"/>
  64. </StackPanel>
  65. </Grid>
  66. </Window>
Code:
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. usingSystem.Windows;
  6. usingSystem.Windows.Controls;
  7. usingSystem.Windows.Data;
  8. usingSystem.Windows.Documents;
  9. usingSystem.Windows.Input;
  10. usingSystem.Windows.Media;
  11. usingSystem.Windows.Media.Imaging;
  12. usingSystem.Windows.Navigation;
  13. usingSystem.Windows.Shapes;
  14. usingSystem.ComponentModel;
  15. namespaceMSSQLDocCreator{
  16. ///<summary>
  17. ///MainWindow.xaml的交互逻辑
  18. ///</summary>
  19. publicpartialclassMainWindow:Window{
  20. publicMainWindow(){
  21. InitializeComponent();
  22. }
  23. ///<summary>
  24. ///Field全选
  25. ///</summary>
  26. privatevoidCheckBox_Click(objectsender,RoutedEventArgse){
  27. ObjectDataProviderprovider=(ObjectDataProvider)this.FindResource("bndOutputFeilds");
  28. List<DocField>fields=provider.DataasList<DocField>;
  29. foreach(DocFieldfieldinfields){
  30. field.IsSelected=((CheckBox)sender).IsChecked.Value;
  31. }
  32. }
  33. privatevoidchkSomeFiels_Click(objectsender,RoutedEventArgse){
  34. CheckSelectedFields();
  35. }
  36. privatevoidCheckSelectedFields(){
  37. ObjectDataProviderprovider=(ObjectDataProvider)this.FindResource("bndOutputFeilds");
  38. List<DocField>fields=provider.DataasList<DocField>;
  39. intcheckedCount=fields.Where(c=>c.IsSelected).Count();
  40. if(checkedCount==0){
  41. chkFieldsAll.IsChecked=false;
  42. }elseif(checkedCount==fields.Count){
  43. chkFieldsAll.IsChecked=true;
  44. }else{
  45. chkFieldsAll.IsChecked=null;
  46. }
  47. }
  48. }
  49. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值