WPF之从资源字典到样式

1.首先声明静态资源

 1 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 2                     xmlns:sys="clr-namespace:System;assembly=mscorlib"
 3                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
 4     //<SolidColorBrush>
 5     <SolidColorBrush x:Key="***" Color="#001122"></SolidColorBrush>
 6     <SolidColorBrush x:Key="***" Color="White"></SolidColorBrush>
 7     <SolidColorBrush x:Key="***" Color="Transparent"></SolidColorBrush>
 8
 9     //<DropShadowEffect>
10     <DropShadowEffect x:Key="***" Color="#001122" BlurRadius="8" ShadowDeph="0"         Direction="0" Opacity="0.7">
11     </DropShadowEffect>
12
13     //<LinearGradientBrush>
14     <LinearGradientBrush x:Key="***" StartPoint="0.5,0" EndPoint="0.5,1">
15         <GradientStop Color="#001122" Offset="0">
16         <GradientStop Color="#112233" Offset="1">
17     </LinearGradientBrush>
18
19     //<FontFamily>
20     <FontFamily x:Key="***">Microsoft Yahei</FontFamily>
21 </ResourceDictionary>

2.声明样式

 1 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 2                     xmlns:local="clr-namespace:Util.Controls"
 3                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
 4     //TextBlock
 5     <Style TargetType="{x:Type TextBlock}">
 6         <Setter Property="Foreground" Value="{StaticResource TextForeGround}"/>
 7         <Setter Property="FontFamily" Value="{StaticResource FontFamily}"/>
 8         <Setter Property="FontSize" Value="{StaticResource FontSize}"/>
 9     </Style>
10
11     //ToolTip
12     <Style Target="{x:Type ToolTip}">
13         <Setter Property="Foreground" Value="{StaticResource TextForeground}"/>
14         <Setter Property="FontFamily" Value="{StaticResource FontFamily}"/>
15         <Setter Property="FontSize" Value="{StaticResource FontSize}"/>
16         <Setter Property="Background" Value="{StaticResource HeaderBackground}"/>
17         <Setter Property="BorderBrush" Value="{StaticResource FocusBorderBrush}"/>
18         <Setter Property="BorderThickness" Value="1"/>
19         <Setter Property="Template">
20             <Setter.Value>
21                 <ControlTemplate TargetType="{x:Type ToolTip}">
22                     <Border CornerRadius="2" BorderThickness="{TemplateBinding BorderThickness}"                        BorderBrush="{TemplateBinding BorderBrush}"                        Background="{TemplateBinding Background}">
23                         <ContentPresenter Margin="8,5,8,5"/>
24                     </Border>
25                 </ControlTemplate>
26             </Setter.Value>
27         </Setter>
28     </Style>
29 </ResourceDictionary>

3.声明样式

1 <ResourceDictionary>
2     <ResourceDictionary.MergeDictionaries>
3         //...
4         <ResourceDictionary Source="pack://application:,,,WpfApplication.Controls;component/Style/Global.xaml">
5         //                          资源问价在当前程序集  当前的命名空间          本地路径:Style/Global.xaml
6         //...
7     </ResourceDictionary.MergeDictionaries>
8 </ResourceDictionary>
时间: 2024-07-30 12:32:22

WPF之从资源字典到样式的相关文章

WPF 动态添加控件以及样式字典的引用(Style introduction)

原文:WPF 动态添加控件以及样式字典的引用(Style introduction) 我们想要达到的结果是,绑定多个Checkbox然后我们还可以获取它是否被选中,其实很简单,我们只要找到那几个关键的对象就可以了. 下面是Ui,其中定义了一个WrapPanel来存放CheckBox,还有两个按钮,用于测试相关功能. <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.c

WPF 精修篇 管理资源字典

原文:WPF 精修篇 管理资源字典 样式太多  每个界面可能需要全局的样式 有没有肯能 WPF 中的样式 像Asp.net中 的CSS一样管理那 有的 有资源字典 BurshDictionary <LinearGradientBrush x:Key="RectFill" EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="BurlyWood" O

WPF合并资源字典

1.合并多个外部资源字典成为本地字典 示例代码 <Page.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="myresourcedictionary1.xaml"/> <ResourceDictionary Source="myresourcedictionary2.xam

WPF资源字典使用

资源字典出现的初衷就在于可以实现多个项目之间的共享资源,资源字典只是一个简单的XAML文档,该文档除了存储希望使用的资源之外,不做任何其它的事情. 1.  创建资源字典 创建资源字典的过程比较简单,只是将需要使用的资源全都包含在一个xaml文件之中即可.如下面的例子(文件名xxx.xaml,与后面的app.xaml文件中的内容相对应): <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/prese

WPF使用资源字典组织资源

转载:http://blog.163.com/[email protected]/blog/static/371405262010111413321728/ 首先在解决方案资源管理器中添加一个或多个资源词典(资源字典),并向多个资源字典中添加对象资源信息.示例中新建了三个资源字典,并向资源字典中添加了对象资源,代码如下. 第一个资源字典:(第一个资源字典命名为MyDictionary1.xaml) <ResourceDictionary xmlns="http://schemas.micr

wpf多程序集之间共享资源字典--CLR名称空间未定义云云

wpf多程序集之间共享资源字典--CLR名称空间未定义云云 分类: WPF 2012-10-28 10:57 1162人阅读 评论(0) 收藏 举报 以下介绍如何创建可用于在多个程序集之间共享的资源字典 1.新建Wpf自定义控件库,名称为MyContorlLib,项目资源结构图如下: 2.打开Themes文件夹中的Generic.xaml文件,在根元素中添加如下名称空间:xmlns:local="clr-namespace:MyContorlLib"因为待会儿要用到该名称空间下的一个类

【WPF学习】第三十五章 资源字典

原文:[WPF学习]第三十五章 资源字典 如果希望在多个项目之间共享资源,可创建资源字典.资源字典只是XAML文档,除了存储希望使用的资源外,不做其他任何事情. 一.创建资源字典 下面是一个资源字典示例,它包含一个资源: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/wi

(WPF)资源字典

资源字典:资源字典出现的初衷就在于可以实现多个项目之间的共享资源,资源字典只是一个简单的XAML文档,                   该文档除了存储希望使用的资源之外,不做任何其它的事情. 资源字典的声明: <ResourceDictionary> <LinearGradientBrush x:Key="fadeBrush"> <GradientStop Color="Red" Offset="0"/>

WPF学习笔记-使用自定义资源字典(style)文件

1.添加资源字典文件style.xmal 2.在资源字典中添加自定义style等 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfTest.resources&