WPF引用外部样式

假设一个应用程序中,某个窗口需要使用样式,但是样式非常多,写在一个窗口中代码分类不方便。最好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. }   

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
WPF是一种用于开发Windows应用程序的技术框架。迅雷窗口样式是指在WPF应用程序中使用类似于迅雷软件的窗口样式。 迅雷窗口样式通常包括以下几个特点: 1. 边框和标题栏:迅雷窗口样式通常具有特殊的边框和标题栏设计,可以给用户带来独特的视觉体验。边框和标题栏可以进行自定义,例如可以设置不规则的边框样式,或者给标题栏添加自定义的图标和按钮。 2. 透明效果:迅雷窗口样式通常采用透明效果,可以显示出背后的桌面或其他窗口内容。这样可以增加应用程序的时尚感和科技感。 3. 强调视图:迅雷窗口样式通常会对某些重要的视图元素进行强调,例如下载进度、操作按钮等。这样可以提升用户对这些重要信息的注意力,使其更易于操作。 4. 动画效果:迅雷窗口样式通常会使用各种动画效果来增加交互的流畅性和美观性,例如弹出菜单、窗口缩放等。这样可以提升用户体验,使应用程序更加生动有趣。 开发WPF应用程序时,可以使用XAML语言来定义窗口样式。通过设置窗口的样式属性和使用自定义的控件模板,可以实现类似迅雷窗口样式的效果。同时,WPF还提供了丰富的视觉效果和动画功能,可以进一步增强迅雷窗口样式的效果。 总之,迅雷窗口样式是一种在WPF应用程序中使用类似于迅雷软件的窗口外观和交互效果的设计风格。通过合理运用WPF技术,可以实现这种样式,并提升用户体验和应用程序的吸引力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值