一键导入
wpf-rule-resourcedictionary-patterns
WPF ResourceDictionary rules: Generic.xaml as MergedDictionaries hub only, per-control style files, resource order.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
WPF ResourceDictionary rules: Generic.xaml as MergedDictionaries hub only, per-control style files, resource order.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
WPF IValueConverter rules: MarkupExtension singleton, pure functions, null/UnsetValue handling, TemplateBinding.
WPF Freezable performance rules: Freeze() all Brush/Pen/Geometry; create-and-freeze in constructor, reuse in OnRender.
WPF MVVM layer-separation rules: no System.Windows in ViewModels, BCL-only types, CommunityToolkit.Mvvm base classes. Preloaded into MVVM-related wpf-dev-pack agents.
Banned wpf-dev-pack patterns (P-001..P-004): ViewModelLocator, code-behind DataContext, Stateless ViewModel, mixing composition paths.
WPF rendering anti-patterns: no InvalidateVisual in loops, no resource allocation in OnRender.
View-ViewModel wiring for CommunityToolkit.Mvvm: Mappings.xaml + implicit DataTemplate (ViewModel First).
| name | wpf-rule-resourcedictionary-patterns |
| description | WPF ResourceDictionary rules: Generic.xaml as MergedDictionaries hub only, per-control style files, resource order. |
| user-invocable | false |
Rules for organizing ResourceDictionary files in WPF custom control libraries.
Generic.xaml must function exclusively as a hub that merges other dictionaries.
Never define inline styles, templates, or resources directly in Generic.xaml.
<!-- Correct: Generic.xaml is only a hub -->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes/Colors.xaml" />
<ResourceDictionary Source="/Themes/MyButton.xaml" />
<ResourceDictionary Source="/Themes/MyTextBox.xaml" />
<ResourceDictionary Source="/Themes/MySlider.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<!-- Prohibited: inline style in Generic.xaml -->
<ResourceDictionary ...>
<Style TargetType="{x:Type local:MyButton}">
...
</Style>
</ResourceDictionary>
Every custom control gets its own XAML file under Themes/:
MyControlLibrary/
├── Themes/
│ ├── Generic.xaml ← hub only, no styles
│ ├── Colors.xaml ← shared color/brush resources
│ ├── MyButton.xaml ← ControlTemplate for MyButton
│ ├── MyTextBox.xaml ← ControlTemplate for MyTextBox
│ └── MySlider.xaml ← ControlTemplate for MySlider
├── MyButton.cs
├── MyTextBox.cs
└── MySlider.cs
Within any single XAML file, define resources in this order:
x:Key named resources (colors, brushes, geometry, converters)Style definitions (which reference the above by StaticResource)<ResourceDictionary ...>
<!-- 1. Named resources first -->
<Color x:Key="PrimaryColor">#FF2196F3</Color>
<SolidColorBrush x:Key="PrimaryBrush" Color="{StaticResource PrimaryColor}" />
<!-- 2. Styles second -->
<Style TargetType="{x:Type local:MyButton}">
<Setter Property="Background" Value="{StaticResource PrimaryBrush}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyButton}">
...
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
| Scenario | Use |
|---|---|
| Resource defined in same file or merged earlier | StaticResource |
| Resource may change at runtime (theme switching) | DynamicResource |
| Resource defined in a later-merged dictionary | DynamicResource |
| ControlTemplate internal references | TemplateBinding (not resource lookup) |
Prefer StaticResource by default — it is resolved at load time and has zero runtime overhead.
Use DynamicResource only when runtime replacement is explicitly required.