c# - 如何仅将 MaterialDesignInXAML 应用于一个控件
问题描述
我想为我的 WPF 应用程序的某些元素使用 MaterialDesignInXAML,但我不想将主题应用于整个应用程序(我对按钮特别感兴趣)
那么如何在不将其应用于 App.xaml 的情况下使用主题:
<Application . . . >
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
解决方案
你应该ResourceDictionary
在你的UserControl
<Button x:Class="MyButton">
<Button.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Button.Resources>
</Button>
并使用MyButton
<MyButton></MyButton>
推荐阅读
- spring-boot - 使用 ElasticSearch 启动 Spring Boot 应用程序时出错
- google-maps - GMapsFX 显示空白地图
- f# - F#递归列表加起来
- javascript - 形成api请求
- json - JSON Schema - 数组内的复杂验证
- node.js - 我可以通过 v8-profiler 分析运行方法吗
- python - 解释 predict_proba,多项式朴素贝叶斯
- reactjs - ReactJS 组件未获取我的 Heroku 托管 API,将获取其他 API
- html - 如何通过 html 从我的电脑加载图像?
- mysql - 优化mysql自连接重叠日期范围