管理用户可编辑的 WPF 样式
我有一些共享样式,它们定义了我的 DataGrid 的外观,并将它们保存在 Styles.xaml 中。
我添加了一个设置页面,这将允许用户更改一些颜色。在该页面上,我有一个示例网格(由于共享资源样式,它自动看起来像其他网格)。在该页面上,用户可以使用颜色选择器来修改属性的颜色,例如行背景颜色、突出显示的行颜色、标题背景以及使用数据驱动转换器应用的其他样式。
我希望将样式应用于设置页面网格,仅用于预览,如果应用,则返回全局样式。
我一直到为每种可配置颜色加载颜色选择器,但不确定将结果应用到什么。
我应该
:将选定的颜色直接应用到网格? (似乎网格只允许我动态分配样式,而不是单独的样式设置器)
b。获取共享资源,复制它,然后在设置网格中交换它,而不是用户“应用”将其交换为共享资源? (我认为这是理想的,但不知道如何做到这一点..
)另一种方法来做到这一点?
I have some shared styles which define how my DataGrids look and i keep them in Styles.xaml
I added a settings page, that will let the user change some of the colors. On that page, I have a sample grid (which automatically looks like the other grids thanks to shared resource styles). On that page, a user can use a color picker, to modify colors of attributes such as row background color, highlighted row colors, header background, and other styles which are applied using data driven converters.
I would like the style to be applied on the settings page grid only for a preview, and if applied, back to global styles.
i am all the way up to the point of loading a color picker for each configurable color, and not sure what to apply the result to.
Should i:
a. apply the selected color directly to the grid? (seems like grid only lets me assign styles dynamically, and not individual style setters)
b. get shared resource, copy it, and swap it in settings grid, than if user "applies" swap it for shared resource? (this one is ideal in my opinion, but not sure how to do this..)
c. another way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我倾向于这样处理:
其中大部分内容都很简单,因此我不会详细介绍。
这是 Styles.xaml 的想法:
这是复制构造 ColorProxy 对象的代码:
以及 ColorProxy 本身:
现在可以使用以下方式编辑 ResourceDictionary 中的颜色:
可以使用 XamlWriter 将编辑后的 ResourceDictionary 转换为字符串以进行存储并重新加载使用 XamlReader。
由 Styles.xaml 生成的主 ResourceDictionary 中的 MergedDictionaries 集合可以通过在旧词典上调用 .Remove() 并在新词典上调用 .Add() 来修改。
可以通过简单地构造一个新的 ResourceDictionary 迭代旧字典中的条目并将它们添加到新字典中来克隆 ResourceDictionaries。
该技术不必限于编辑颜色。可以创建任何类型的代理对象,包括由绑定中的转换器处理数据转换的通用代理对象。
I would be inclined to handle it this way:
Most of this is straightforward, so I won't go into too much detail.
Here is the idea of Styles.xaml:
Here is code to copy the construct the ColorProxy objects:
and ColorProxy itself:
The colors in the ResourceDictionary can now be edited with:
The edited ResourceDictionary can be converted to a string for storage using XamlWriter and reloaded using XamlReader.
The MergedDictionaries collection in the main ResourceDictionary generated by Styles.xaml can be modified by calling .Remove() on the old dictionary and .Add() on the new one.
ResourceDictionaries can be cloned by simply constructing a new ResourceDictionary th iterating the entries in the old dictionary and adding them to the new one.
This technique need not be limited to editing colors. Any kind of proxy objects can be created, including generic ones where the data conversion is handled by a converter in the binding.