WPF,控制。依赖属性值的堆栈

发布于 2024-12-12 02:58:05 字数 530 浏览 1 评论 0原文

我一直遇到一个非常微不足道的问题(乍一看)。 我的情况的简化版本如下所示:

我希望将多个背景(例如)颜色应用于相同的Control,并且能够仅清除其中一些颜色(通过健康)状况)。 WPF 不提供这样的功能,我只能为每个 DP 设置一个值。

因此,我想依次将 Yellow 然后 Green 然后 Red 颜色应用到 Control 对象,然后能够重置红色,以便 Control 将具有绿色背景(另一方面,重置绿色会留下背景红色)。看起来很简单,你曾经创建过这样的控件装饰器吗?我相信 DP 内部使用了这种行为,但没有公共 API。

请不要建议使用触发器或任何其他类型的条件 DP 设置,我确实需要为单个 DP 设置多个值并能够管理它们

I've stuck with one pretty trivial problem (at first look).
Simplified version of my situation looks like this:

I want multiple Background(for example) colors to be applied to SAME Control and to be able to clear only some of them (by condition). WPF does not offer such capability, I can set only one value for each DP.

So, I want to apply Yellow then Green then Red colors successively to Control object and then be able to reset Red so that Control will be with Green background (on the other hand, reseting Green leaves background Red). Looks pretty simple, have you ever created such Decorator for Control? I believe DP internally use such behavior, but there are no public APIs for it.

Please, do not suggest using triggers or any other kinds of conditional DP setting, I really need to set multiple values for single DP and to be able to manage them

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖伴 2024-12-19 02:58:05

说“不建议”不是一个好主意...无论如何,这是不可能的,并且 WPF 不这样做,您可能的意思是值 优先级 但是这个List 是固定的,你不能在中间添加另一个层,有充分的理由。整个系统都依赖于此。否则,您无法设置一个值并使用触发器来覆盖它,并且不需要负责重置该值。如果人们开始搞乱这个列表,动画、样式继承等也将不可能实现。

所以你不喜欢触发这个想法......为什么?正是为了这个。
好的,如果您不喜欢它,可以为 Background1Background2 等添加附加属性吗?您还可以将 Background 作为附加属性并使其可继承。您可以创建附加行为,监听属性更改并修改背景颜色。您可以使用 MultiBinding 或 PriorityBinding。
如果不需要明显的解决方案,那么很多事情都是可能的:)

Its not a good idea to say "do not suggest" ... Anyway, Well its not possible, and WPF does not do this, what you might meant is the value precendence but this List is fixed, you can't just add another layer in between, for good reasons. The whole system relys on that. Otherwise you could not set one value and use triggers to override it and don't need to take care of resetting the value. Also animations, Style inheritence etc. wouldn't be possible if people start to mess with this list.

So you don't like the trigger idea ... Why? Its exactly for that.
Ok, if you don't like it, how about adding an attached property for Background1, Background2 etc. You also could make Background as an attached property and make it inheritable. You could make an attached behavior, listen to property changes and modify your Background color. You could use a MultiBinding or PriorityBinding.
Well alot of stuff is possible if the obvious solution is not desired :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文