Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF将笔刷从ResourceDictionary设置为ViewModel中的属性_C#_Wpf_Mvvm_Viewmodel_Resourcedictionary - Fatal编程技术网

C# WPF将笔刷从ResourceDictionary设置为ViewModel中的属性

C# WPF将笔刷从ResourceDictionary设置为ViewModel中的属性,c#,wpf,mvvm,viewmodel,resourcedictionary,C#,Wpf,Mvvm,Viewmodel,Resourcedictionary,我正在用按钮创建一个视图,我想在单击时更改它们的颜色。 我希望按钮有一个默认的颜色,第一次点击会将其颜色更改为另一种颜色 为了做到这一点,我想保持它干净,所以,我把画笔保存在一本资源字典中 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/

我正在用按钮创建一个视图,我想在单击时更改它们的颜色。 我希望按钮有一个默认的颜色,第一次点击会将其颜色更改为另一种颜色

为了做到这一点,我想保持它干净,所以,我把画笔保存在一本资源字典中

<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>
现在我想将我的字典中的画笔插入这个属性,如何将画笔从字典中提取到视图模型中


提前感谢所有的帮助者

如果在代码隐藏中使用此代码,则此代码将起作用:

button.Background = (Brush)FindResource("ButtonNormalBackgroundBrush");
但是!你是说你想把它弄干净

对于MVVM,我将按钮笔刷绑定到属性


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

可能重复“我希望在整个应用程序中使用相同的颜色,因此我认为最好从字典中获取”。不,相反的情况是正确的:想要在整个应用程序中使用相同的颜色->将它们放入资源/资源字典YMVVM并不意味着“将我所有的UI代码推到视图模型中”。为什么要求助于代码隐藏
Background=“{StaticResource按钮normalbackgroundbrush}”
是的,你说得对。他只是询问如何在VM中访问ResourceDictionary来解决他的问题。我只是想指出,这在CodeBehind中是很容易实现的,但正如我所说的,这仍然不是很好的解决方案。在本例中,使用触发器和静态资源的绑定比您所指出的要多。
button.Background = (Brush)FindResource("ButtonNormalBackgroundBrush");