gpt4 book ai didi

c# - 使用 MVVM 在代码中动态设置资源

转载 作者:行者123 更新时间:2023-12-03 10:26:21 24 4
gpt4 key购买 nike

所以基本上我想做的是使用 MVVM 模式通过代码设置一个 DynamicResource 。

这些是我的资源:

<SolidColorBrush x:Key="UserGroupUserBrush" Color="Transparent"/>
<SolidColorBrush x:Key="UserGroupSetterBrush" Color="Yellow"/>
<SolidColorBrush x:Key="UserGrougShiftLeaderBrush" Color="{StaticResource ZFBlueColor}"/>
<SolidColorBrush x:Key="UserGroupTeamLeaderBrush" Color="Black"/>

在这里我想设置资源:
<Ellipse DataContext="{vw:AdapterBinding UserManagementAdapter}" Fill="{DynamicResource UserGroupSetterBrush}" Height="20" Width="20" Margin="0,0,5,0"/>

所以假设我想在我的代码中将资源“UserGroupSetterBrush”更改为“UserGroupUserBrush”。使用 MVVM 模式执行此操作的正确方法是什么?

最佳答案

What is the correct way to do this using the MVVM pattern?



在某些 View 或某些与 View 相关的类的代码隐藏中。资源和画笔只是 View 的一部分。 View 模型根本不知道也不关心这些,因此在 MVVM 和非 MVVM 应用程序之间使用或设置资源基本上没有区别。

View 模型不应更改 Fill Ellipse 的属性(property).它可能会更改 Fill 的源属性。属性是绑定(bind)的,但是你不应该使用 DynamicResource 设置属性但将其绑定(bind)到 Brush View 模型的 source 属性,并在 View 模型中设置此属性。

关于c# - 使用 MVVM 在代码中动态设置资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45163407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com