WPF 将 Brush 从 ResourceDictionary 设置为 ViewModel 中的 属性

WPF setting Brush from ResourceDictionary to a property in a ViewModel

我正在制作一个带有按钮的视图,我想在单击时更改它们的颜色。 我希望按钮具有默认颜色,并且在第一次单击时会将它们的颜色更改为另一种颜色。

为了做到这一点,我想保持清洁,所以我将画笔保存在 resourceDictionary 中。

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

     <SolidColorBrush x:Key="WeekCalendarDefaultCellColor" Color="#FFE5CC"/>
     <SolidColorBrush x:Key="WeekCalendarClickCellColor" Color="#FFFF00"/>

</ResourceDictionary>

对于 MVVM,我将我的按钮画笔绑定到 属性(如果我自己设置颜色,绑定会起作用,但我想在整个应用程序中使用相同的颜色,所以我认为最好从字典里查)

public SolidColorBrush CurrentBrush =//????;

现在我想将字典中的画笔插入到此属性,如何从字典中获取画笔到视图模型?

在此先感谢所有帮助者!

如果您在后面的代码中使用此代码,则该代码会起作用:

button.Background = (Brush)FindResource("ButtonNormalBackgroundBrush");

但是!你是说你想把它弄干净。

for MVVM, I bind my button brush to a property

您不应将按钮刷绑定到 VM 属性。画笔是GUI(View in MVVM)的一部分。 VM 应该包含某种状态,例如 bool 或 enum 等。您可以以按钮样式读取此状态并使用触发器更改背景。