如何使用 C# Code Behind 从 Material Design in XAML 工具包中检索托盘刷之一?

How do you retrieve one of the pallet brushes from the Material Design in XAML toolkit using C# Code Behind?

我可怜地试图找回一个:

MaterialDesign.Themes.Wpf.Current.Brushes.MaterialDesignBody

(这当然行不通。)

画笔的文档是here. The brushes themselves appear to be located here;我可以在 xaml 中找到它们,但不知道如何在 C# 中找到它们。

如果有人知道哪个画笔控制文本颜色,我也将不胜感激。从当前选定的主题中检索画笔的奖励积分。

这些是资源。

FrameworkElement 中,您可以使用 FindResource 方法检索:

SolidColorBrush MaterialDesignBody = FindResource("MaterialDesignBody") as SolidColorBrush;

您也可以使用 ApplicationTryFindResource 方法 class:

SolidColorBrush MaterialDesignBody = Application.Current.TryFindResource("MaterialDesignBody") as SolidColorBrush;

这将为您提供当前所选主题的画笔,前提是您已将其合并到 App.xaml 文件中,例如:

<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml" />

由于资源被插入到应用程序资源字典中,因此可以执行以下操作:

Application.Current.TryFindResource("PrimaryHueLightBrush")