├── .gitattributes
├── .gitignore
├── LICENSE
├── README.md
├── WpfMultiStyle.Test
├── App.config
├── App.xaml
├── App.xaml.cs
├── MainWindow.xaml
├── MainWindow.xaml.cs
├── Properties
│ ├── AssemblyInfo.cs
│ ├── Resources.Designer.cs
│ ├── Resources.resx
│ ├── Settings.Designer.cs
│ └── Settings.settings
└── WpfMultiStyle.Test.csproj
├── WpfMultiStyle.sln
├── WpfMultiStyle
├── Apply.cs
├── InternalExtensions.cs
├── MultiStyleExtension.cs
├── Properties
│ └── AssemblyInfo.cs
├── WpfMultiStyle.csproj
└── WpfMultiStyle.nuspec
└── demo.png
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.userosscache
8 | *.sln.docstates
9 |
10 | # User-specific files (MonoDevelop/Xamarin Studio)
11 | *.userprefs
12 |
13 | # Build results
14 | [Dd]ebug/
15 | [Dd]ebugPublic/
16 | [Rr]elease/
17 | [Rr]eleases/
18 | [Xx]64/
19 | [Xx]86/
20 | [Bb]uild/
21 | bld/
22 | [Bb]in/
23 | [Oo]bj/
24 |
25 | # Visual Studio 2015 cache/options directory
26 | .vs/
27 | # Uncomment if you have tasks that create the project's static files in wwwroot
28 | #wwwroot/
29 |
30 | # MSTest test Results
31 | [Tt]est[Rr]esult*/
32 | [Bb]uild[Ll]og.*
33 |
34 | # NUNIT
35 | *.VisualState.xml
36 | TestResult.xml
37 |
38 | # Build Results of an ATL Project
39 | [Dd]ebugPS/
40 | [Rr]eleasePS/
41 | dlldata.c
42 |
43 | # DNX
44 | project.lock.json
45 | artifacts/
46 |
47 | *_i.c
48 | *_p.c
49 | *_i.h
50 | *.ilk
51 | *.meta
52 | *.obj
53 | *.pch
54 | *.pdb
55 | *.pgc
56 | *.pgd
57 | *.rsp
58 | *.sbr
59 | *.tlb
60 | *.tli
61 | *.tlh
62 | *.tmp
63 | *.tmp_proj
64 | *.log
65 | *.vspscc
66 | *.vssscc
67 | .builds
68 | *.pidb
69 | *.svclog
70 | *.scc
71 |
72 | # Chutzpah Test files
73 | _Chutzpah*
74 |
75 | # Visual C++ cache files
76 | ipch/
77 | *.aps
78 | *.ncb
79 | *.opendb
80 | *.opensdf
81 | *.sdf
82 | *.cachefile
83 | *.VC.db
84 |
85 | # Visual Studio profiler
86 | *.psess
87 | *.vsp
88 | *.vspx
89 | *.sap
90 |
91 | # TFS 2012 Local Workspace
92 | $tf/
93 |
94 | # Guidance Automation Toolkit
95 | *.gpState
96 |
97 | # ReSharper is a .NET coding add-in
98 | _ReSharper*/
99 | *.[Rr]e[Ss]harper
100 | *.DotSettings.user
101 |
102 | # JustCode is a .NET coding add-in
103 | .JustCode
104 |
105 | # TeamCity is a build add-in
106 | _TeamCity*
107 |
108 | # DotCover is a Code Coverage Tool
109 | *.dotCover
110 |
111 | # NCrunch
112 | _NCrunch_*
113 | .*crunch*.local.xml
114 | nCrunchTemp_*
115 |
116 | # MightyMoose
117 | *.mm.*
118 | AutoTest.Net/
119 |
120 | # Web workbench (sass)
121 | .sass-cache/
122 |
123 | # Installshield output folder
124 | [Ee]xpress/
125 |
126 | # DocProject is a documentation generator add-in
127 | DocProject/buildhelp/
128 | DocProject/Help/*.HxT
129 | DocProject/Help/*.HxC
130 | DocProject/Help/*.hhc
131 | DocProject/Help/*.hhk
132 | DocProject/Help/*.hhp
133 | DocProject/Help/Html2
134 | DocProject/Help/html
135 |
136 | # Click-Once directory
137 | publish/
138 |
139 | # Publish Web Output
140 | *.[Pp]ublish.xml
141 | *.azurePubxml
142 |
143 | # TODO: Un-comment the next line if you do not want to checkin
144 | # your web deploy settings because they may include unencrypted
145 | # passwords
146 | #*.pubxml
147 | *.publishproj
148 |
149 | # NuGet Packages
150 | *.nupkg
151 | # The packages folder can be ignored because of Package Restore
152 | **/packages/*
153 | # except build/, which is used as an MSBuild target.
154 | !**/packages/build/
155 | # Uncomment if necessary however generally it will be regenerated when needed
156 | #!**/packages/repositories.config
157 | # NuGet v3's project.json files produces more ignoreable files
158 | *.nuget.props
159 | *.nuget.targets
160 |
161 | # Microsoft Azure Build Output
162 | csx/
163 | *.build.csdef
164 |
165 | # Microsoft Azure Emulator
166 | ecf/
167 | rcf/
168 |
169 | # Microsoft Azure ApplicationInsights config file
170 | ApplicationInsights.config
171 |
172 | # Windows Store app package directory
173 | AppPackages/
174 | BundleArtifacts/
175 |
176 | # Visual Studio cache files
177 | # files ending in .cache can be ignored
178 | *.[Cc]ache
179 | # but keep track of directories ending in .cache
180 | !*.[Cc]ache/
181 |
182 | # Others
183 | ClientBin/
184 | [Ss]tyle[Cc]op.*
185 | ~$*
186 | *~
187 | *.dbmdl
188 | *.dbproj.schemaview
189 | *.pfx
190 | *.publishsettings
191 | node_modules/
192 | orleans.codegen.cs
193 |
194 | # RIA/Silverlight projects
195 | Generated_Code/
196 |
197 | # Backup & report files from converting an old project file
198 | # to a newer Visual Studio version. Backup files are not needed,
199 | # because we have git ;-)
200 | _UpgradeReport_Files/
201 | Backup*/
202 | UpgradeLog*.XML
203 | UpgradeLog*.htm
204 |
205 | # SQL Server files
206 | *.mdf
207 | *.ldf
208 |
209 | # Business Intelligence projects
210 | *.rdl.data
211 | *.bim.layout
212 | *.bim_*.settings
213 |
214 | # Microsoft Fakes
215 | FakesAssemblies/
216 |
217 | # GhostDoc plugin setting file
218 | *.GhostDoc.xml
219 |
220 | # Node.js Tools for Visual Studio
221 | .ntvs_analysis.dat
222 |
223 | # Visual Studio 6 build log
224 | *.plg
225 |
226 | # Visual Studio 6 workspace options file
227 | *.opt
228 |
229 | # Visual Studio LightSwitch build output
230 | **/*.HTMLClient/GeneratedArtifacts
231 | **/*.DesktopClient/GeneratedArtifacts
232 | **/*.DesktopClient/ModelManifest.xml
233 | **/*.Server/GeneratedArtifacts
234 | **/*.Server/ModelManifest.xml
235 | _Pvt_Extensions
236 |
237 | # LightSwitch generated files
238 | GeneratedArtifacts/
239 | ModelManifest.xml
240 |
241 | # Paket dependency manager
242 | .paket/paket.exe
243 |
244 | # FAKE - F# Make
245 | .fake/
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2016 Andy
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Multi-Style for WPF
2 |
3 | WpfMultiStyle 是用于 WPF 中,使控件同时应用多个 Style,类似于 HTML 中 `class="class1 class2 class3"`。
4 |
5 | ## 使用
6 |
7 | ### 引入命名空间[Nuget](https://www.nuget.org/packages/WpfMultiStyle/)
8 | ```C#
9 | xmlns:wms="clr-namespace:WpfMultiStyle;assembly=WpfMultiStyle"
10 | ```
11 |
12 | ### 应用样式
13 | 目前有两种方式。一种是通过MarkupExtension扩展,这种有点儿缺陷,在设计时不能直接实时显示效果,需要把 Style 剪切再粘贴才会显示真正的效果。一种是通过附加属性,这个是实时显示效果的。
14 | 以`Button` 为例:
15 | ```C#
16 |
17 |
18 | ```
19 |
20 | ### 效果
21 | 
22 |
23 | ## 参考
24 | [How to apply multiple styles in WPF](http://stackoverflow.com/questions/16096/how-to-apply-multiple-styles-in-wpf)
25 |
--------------------------------------------------------------------------------
/WpfMultiStyle.Test/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/WpfMultiStyle.Test/App.xaml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/WpfMultiStyle.Test/App.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Configuration;
4 | using System.Data;
5 | using System.Linq;
6 | using System.Threading.Tasks;
7 | using System.Windows;
8 |
9 | namespace WpfMultiStyle.Test
10 | {
11 | ///
12 | /// App.xaml 的交互逻辑
13 | ///
14 | public partial class App : Application
15 | {
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/WpfMultiStyle.Test/MainWindow.xaml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
26 |
27 |
69 |
70 |
112 |
113 |
117 |
118 |
122 |
123 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
--------------------------------------------------------------------------------
/WpfMultiStyle.Test/MainWindow.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using System.Windows;
7 | using System.Windows.Controls;
8 | using System.Windows.Data;
9 | using System.Windows.Documents;
10 | using System.Windows.Input;
11 | using System.Windows.Media;
12 | using System.Windows.Media.Imaging;
13 | using System.Windows.Navigation;
14 | using System.Windows.Shapes;
15 |
16 | namespace WpfMultiStyle.Test
17 | {
18 | ///
19 | /// MainWindow.xaml 的交互逻辑
20 | ///
21 | public partial class MainWindow : Window
22 | {
23 | public MainWindow()
24 | {
25 | InitializeComponent();
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/WpfMultiStyle.Test/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Resources;
3 | using System.Runtime.CompilerServices;
4 | using System.Runtime.InteropServices;
5 | using System.Windows;
6 |
7 | // 有关程序集的一般信息由以下
8 | // 控制。更改这些特性值可修改
9 | // 与程序集关联的信息。
10 | [assembly: AssemblyTitle("WpfMultiStyle.Test")]
11 | [assembly: AssemblyDescription("")]
12 | [assembly: AssemblyConfiguration("")]
13 | [assembly: AssemblyCompany("")]
14 | [assembly: AssemblyProduct("WpfMultiStyle.Test")]
15 | [assembly: AssemblyCopyright("Copyright © 2016")]
16 | [assembly: AssemblyTrademark("")]
17 | [assembly: AssemblyCulture("")]
18 |
19 | //将 ComVisible 设置为 false 将使此程序集中的类型
20 | //对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型,
21 | //请将此类型的 ComVisible 特性设置为 true。
22 | [assembly: ComVisible(false)]
23 |
24 | //若要开始生成可本地化的应用程序,请
25 | // 中的 .csproj 文件中
26 | //例如,如果您在源文件中使用的是美国英语,
27 | //使用的是美国英语,请将 设置为 en-US。 然后取消
28 | //对以下 NeutralResourceLanguage 特性的注释。 更新
29 | //以下行中的“en-US”以匹配项目文件中的 UICulture 设置。
30 |
31 | //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
32 |
33 |
34 | [assembly: ThemeInfo(
35 | ResourceDictionaryLocation.None, //主题特定资源词典所处位置
36 | //(当资源未在页面
37 | //或应用程序资源字典中找到时使用)
38 | ResourceDictionaryLocation.SourceAssembly //常规资源词典所处位置
39 | //(当资源未在页面
40 | //、应用程序或任何主题专用资源字典中找到时使用)
41 | )]
42 |
43 |
44 | // 程序集的版本信息由下列四个值组成:
45 | //
46 | // 主版本
47 | // 次版本
48 | // 生成号
49 | // 修订号
50 | //
51 | //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
52 | // 方法是按如下所示使用“*”: :
53 | // [assembly: AssemblyVersion("1.0.*")]
54 | [assembly: AssemblyVersion("1.0.0.0")]
55 | [assembly: AssemblyFileVersion("1.0.0.0")]
56 |
--------------------------------------------------------------------------------
/WpfMultiStyle.Test/Properties/Resources.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // 此代码由工具生成。
4 | // 运行时版本: 4.0.30319.42000
5 | //
6 | // 对此文件的更改可能导致不正确的行为,如果
7 | // 重新生成代码,则所做更改将丢失。
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace WpfMultiStyle.Test.Properties
12 | {
13 |
14 |
15 | ///
16 | /// 强类型资源类,用于查找本地化字符串等。
17 | ///
18 | // 此类是由 StronglyTypedResourceBuilder
19 | // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
20 | // 若要添加或删除成员,请编辑 .ResX 文件,然后重新运行 ResGen
21 | // (以 /str 作为命令选项),或重新生成 VS 项目。
22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
25 | internal class Resources
26 | {
27 |
28 | private static global::System.Resources.ResourceManager resourceMan;
29 |
30 | private static global::System.Globalization.CultureInfo resourceCulture;
31 |
32 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
33 | internal Resources()
34 | {
35 | }
36 |
37 | ///
38 | /// 返回此类使用的缓存 ResourceManager 实例。
39 | ///
40 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
41 | internal static global::System.Resources.ResourceManager ResourceManager
42 | {
43 | get
44 | {
45 | if ((resourceMan == null))
46 | {
47 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("WpfMultiStyle.Test.Properties.Resources", typeof(Resources).Assembly);
48 | resourceMan = temp;
49 | }
50 | return resourceMan;
51 | }
52 | }
53 |
54 | ///
55 | /// 覆盖当前线程的 CurrentUICulture 属性
56 | /// 使用此强类型的资源类的资源查找。
57 | ///
58 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
59 | internal static global::System.Globalization.CultureInfo Culture
60 | {
61 | get
62 | {
63 | return resourceCulture;
64 | }
65 | set
66 | {
67 | resourceCulture = value;
68 | }
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/WpfMultiStyle.Test/Properties/Resources.resx:
--------------------------------------------------------------------------------
1 |
2 |
3 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 | text/microsoft-resx
107 |
108 |
109 | 2.0
110 |
111 |
112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
113 |
114 |
115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
116 |
117 |
--------------------------------------------------------------------------------
/WpfMultiStyle.Test/Properties/Settings.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.42000
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace WpfMultiStyle.Test.Properties
12 | {
13 |
14 |
15 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
16 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
17 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
18 | {
19 |
20 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
21 |
22 | public static Settings Default
23 | {
24 | get
25 | {
26 | return defaultInstance;
27 | }
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/WpfMultiStyle.Test/Properties/Settings.settings:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/WpfMultiStyle.Test/WpfMultiStyle.Test.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {57A6355B-58A6-49D2-AA1B-422CD3CA4E57}
8 | WinExe
9 | Properties
10 | WpfMultiStyle.Test
11 | WpfMultiStyle.Test
12 | v4.5
13 | 512
14 | {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
15 | 4
16 |
17 |
18 | AnyCPU
19 | true
20 | full
21 | false
22 | bin\Debug\
23 | DEBUG;TRACE
24 | prompt
25 | 4
26 |
27 |
28 | AnyCPU
29 | pdbonly
30 | true
31 | bin\Release\
32 | TRACE
33 | prompt
34 | 4
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 | 4.0
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 | MSBuild:Compile
55 | Designer
56 |
57 |
58 | MSBuild:Compile
59 | Designer
60 |
61 |
62 | App.xaml
63 | Code
64 |
65 |
66 | MainWindow.xaml
67 | Code
68 |
69 |
70 |
71 |
72 | Code
73 |
74 |
75 | True
76 | True
77 | Resources.resx
78 |
79 |
80 | True
81 | Settings.settings
82 | True
83 |
84 |
85 | ResXFileCodeGenerator
86 | Resources.Designer.cs
87 |
88 |
89 | SettingsSingleFileGenerator
90 | Settings.Designer.cs
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 | {862053ed-9901-4fe5-8ab1-887057d0461a}
100 | WpfMultiStyle
101 |
102 |
103 |
104 |
111 |
--------------------------------------------------------------------------------
/WpfMultiStyle.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 14
4 | VisualStudioVersion = 14.0.25420.1
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WpfMultiStyle", "WpfMultiStyle\WpfMultiStyle.csproj", "{862053ED-9901-4FE5-8AB1-887057D0461A}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WpfMultiStyle.Test", "WpfMultiStyle.Test\WpfMultiStyle.Test.csproj", "{57A6355B-58A6-49D2-AA1B-422CD3CA4E57}"
9 | EndProject
10 | Global
11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
12 | Debug|Any CPU = Debug|Any CPU
13 | Release|Any CPU = Release|Any CPU
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {862053ED-9901-4FE5-8AB1-887057D0461A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17 | {862053ED-9901-4FE5-8AB1-887057D0461A}.Debug|Any CPU.Build.0 = Debug|Any CPU
18 | {862053ED-9901-4FE5-8AB1-887057D0461A}.Release|Any CPU.ActiveCfg = Release|Any CPU
19 | {862053ED-9901-4FE5-8AB1-887057D0461A}.Release|Any CPU.Build.0 = Release|Any CPU
20 | {57A6355B-58A6-49D2-AA1B-422CD3CA4E57}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21 | {57A6355B-58A6-49D2-AA1B-422CD3CA4E57}.Debug|Any CPU.Build.0 = Debug|Any CPU
22 | {57A6355B-58A6-49D2-AA1B-422CD3CA4E57}.Release|Any CPU.ActiveCfg = Release|Any CPU
23 | {57A6355B-58A6-49D2-AA1B-422CD3CA4E57}.Release|Any CPU.Build.0 = Release|Any CPU
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | EndGlobal
29 |
--------------------------------------------------------------------------------
/WpfMultiStyle/Apply.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using System.Windows;
7 |
8 | namespace WpfMultiStyle
9 | {
10 | ///
11 | /// 通过附加属性使元素应用多个样式。
12 | ///
13 | public class Apply
14 | {
15 | public static string GetMultiStyle(DependencyObject obj)
16 | {
17 | return (string)obj.GetValue(MultiStyleProperty);
18 | }
19 |
20 | public static void SetMultiStyle(DependencyObject obj, string value)
21 | {
22 | obj.SetValue(MultiStyleProperty, value);
23 | }
24 |
25 | public static readonly DependencyProperty MultiStyleProperty = DependencyProperty.RegisterAttached("MultiStyle", typeof(string), typeof(Apply), new PropertyMetadata(null, new PropertyChangedCallback(OnMultiStyleChanged)));
26 |
27 | public static void OnMultiStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
28 | {
29 | var newValue = (string)e.NewValue;
30 | var fe = d as FrameworkElement;
31 | Style result = new Style();
32 |
33 | if (newValue.HasValue())
34 | {
35 | var styleKeys = newValue.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
36 |
37 | foreach (var key in styleKeys)
38 | {
39 | var style = fe.TryFindResource(key) as Style;
40 |
41 | if (style != null)
42 | {
43 | result.Merge(style);
44 | }
45 | }
46 |
47 | fe.Style = result;
48 | }
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/WpfMultiStyle/InternalExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Windows;
5 |
6 | namespace WpfMultiStyle
7 | {
8 | ///
9 | /// 一些扩展方法。
10 | ///
11 | internal static class InternalExtensions
12 | {
13 | ///
14 | /// 合并指定的 。
15 | ///
16 | ///
17 | ///
18 | public static void Merge(this Style style1, Style style2)
19 | {
20 | if (style1 == null)
21 | {
22 | throw new ArgumentNullException("style1");
23 | }
24 | if (style2 == null)
25 | {
26 | throw new ArgumentNullException("style2");
27 | }
28 |
29 | if (style1.TargetType.IsAssignableFrom(style2.TargetType))
30 | {
31 | style1.TargetType = style2.TargetType;
32 | }
33 |
34 | if (style2.BasedOn != null)
35 | {
36 | Merge(style1, style2.BasedOn);
37 | }
38 |
39 | foreach (SetterBase currentSetter in style2.Setters)
40 | {
41 | style1.Setters.Add(currentSetter);
42 | }
43 |
44 | foreach (TriggerBase currentTrigger in style2.Triggers)
45 | {
46 | style1.Triggers.Add(currentTrigger);
47 | }
48 |
49 | // This code is only needed when using DynamicResources.
50 | foreach (object key in style2.Resources.Keys)
51 | {
52 | style1.Resources[key] = style2.Resources[key];
53 | }
54 | }
55 |
56 | ///
57 | /// 集合是否有值。
58 | ///
59 | ///
60 | ///
61 | ///
62 | public static bool HasValue(this IEnumerable source)
63 | {
64 | return source != null && source.Any();
65 | }
66 |
67 | public static ResourceDictionary FindResourceDictionary(this Style style, object resourceKey)
68 | {
69 | if (style.Resources != null && style.Resources.Contains(resourceKey))
70 | {
71 | return style.Resources;
72 | }
73 | if (style.BasedOn != null)
74 | {
75 | return style.BasedOn.FindResourceDictionary(resourceKey);
76 | }
77 | return null;
78 | }
79 |
80 | public static void Merge(this ResourceDictionary resourceDictionary, ResourceDictionary mergee)
81 | {
82 | if (resourceDictionary != null && mergee != null && mergee.Count > 0)
83 | {
84 | foreach (var key in mergee.Keys)
85 | {
86 | if (!resourceDictionary.Contains(key))
87 | {
88 | resourceDictionary.Add(key, mergee[key]);
89 | }
90 | }
91 | }
92 | }
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/WpfMultiStyle/MultiStyleExtension.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Windows;
4 | using System.Windows.Markup;
5 | using System.Xaml;
6 | using System.Linq;
7 |
8 | namespace WpfMultiStyle
9 | {
10 | ///
11 | /// 实现一个标记扩展,该标记扩展支持根据 XAML 制作的多个静态(XAML 加载时) 资源引用。
12 | ///
13 | [MarkupExtensionReturnType(typeof(Style))]
14 | public class MultiStyleExtension : MarkupExtension
15 | {
16 | private string[] _internalResourceKeys = new string[0];
17 | private string _resourceKeys;
18 |
19 | [ConstructorArgument("resourceKeys")]
20 | public string ResourceKeys
21 | {
22 | get
23 | {
24 | return _resourceKeys;
25 | }
26 |
27 | set
28 | {
29 | _resourceKeys = value;
30 |
31 | if (_resourceKeys.HasValue())
32 | {
33 | _internalResourceKeys = _resourceKeys.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
34 | }
35 | else
36 | {
37 | _internalResourceKeys = new string[0];
38 | }
39 | }
40 | }
41 |
42 | ///
43 | /// 构造方法。
44 | ///
45 | public MultiStyleExtension()
46 | {
47 |
48 | }
49 |
50 | ///
51 | /// 构造方法。
52 | ///
53 | /// 多个资源字典多个 Key
54 | public MultiStyleExtension(string resourceKeys)
55 | {
56 | ResourceKeys = resourceKeys;
57 | }
58 |
59 | ///
60 | /// 返回一个应在此扩展应用的属性上设置的对象。对于 ,这是在资源字典中查找的多个 对象,并合并这些对象,其中要查找的对象由 标识。
61 | ///
62 | /// 可以为标记扩展提供服务的对象。
63 | /// 要在计算标记扩展提供的值的属性上设置的对象值。
64 | public override object ProvideValue(IServiceProvider serviceProvider)
65 | {
66 | Style resultStyle = new Style();
67 |
68 | IProvideValueTarget service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
69 | var fe = service.TargetObject as FrameworkElement;
70 |
71 | if (fe != null)
72 | {
73 | foreach (string resourceKey in _internalResourceKeys)
74 | {
75 | Style currentStyle = fe.TryFindResource(resourceKey) as Style;
76 |
77 | // 忽略无效的 Style
78 | if (currentStyle != null)
79 | {
80 | resultStyle.Merge(currentStyle);
81 | }
82 | }
83 | }
84 |
85 | return resultStyle;
86 | }
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/WpfMultiStyle/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // 有关程序集的一般信息由以下
6 | // 控制。更改这些特性值可修改
7 | // 与程序集关联的信息。
8 | [assembly: AssemblyTitle("WpfMultiStyle")]
9 | [assembly: AssemblyDescription("Apply multiple styles to one element for WPF.")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("Px_Andy")]
12 | [assembly: AssemblyProduct("WpfMultiStyle")]
13 | [assembly: AssemblyCopyright("Copyright © Px_Andy 2016")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | //将 ComVisible 设置为 false 将使此程序集中的类型
18 | //对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型,
19 | //请将此类型的 ComVisible 特性设置为 true。
20 | [assembly: ComVisible(false)]
21 |
22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
23 | [assembly: Guid("862053ed-9901-4fe5-8ab1-887057d0461a")]
24 |
25 | // 程序集的版本信息由下列四个值组成:
26 | //
27 | // 主版本
28 | // 次版本
29 | // 生成号
30 | // 修订号
31 | //
32 | //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
33 | // 方法是按如下所示使用“*”: :
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("2.0.0.0")]
36 | [assembly: AssemblyFileVersion("2.0.0.0")]
37 |
--------------------------------------------------------------------------------
/WpfMultiStyle/WpfMultiStyle.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {862053ED-9901-4FE5-8AB1-887057D0461A}
8 | Library
9 | Properties
10 | WpfMultiStyle
11 | WpfMultiStyle
12 | v4.5
13 | 512
14 |
15 |
16 | true
17 | full
18 | false
19 | bin\Debug\
20 | DEBUG;TRACE
21 | prompt
22 | 4
23 |
24 |
25 | pdbonly
26 | true
27 | bin\Release\
28 | TRACE
29 | prompt
30 | 4
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
55 |
--------------------------------------------------------------------------------
/WpfMultiStyle/WpfMultiStyle.nuspec:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $id$
5 | $version$
6 | $title$
7 | PxAndy
8 | $author$
9 | https://github.com/PxAndy/WpfMultiStyle/blob/master/LICENSE
10 | https://github.com/PxAndy/WpfMultiStyle
11 | false
12 | $description$
13 | 1. 优化 MultiStyleExtension。 2. 新增附加属性方式实现。
14 | Copyright 2016
15 | WPF Multi-Style
16 |
17 |
--------------------------------------------------------------------------------
/demo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PxAndy/WpfMultiStyle/5b52469cb9c49a9df49572c065df5c49602f4f19/demo.png
--------------------------------------------------------------------------------