如何阻止属性值沿着 xaml 中的元素树向下流动?
假设我有一些已被禁用的控件。它包含一堆元素,但我希望其中一个子元素保持启用状态。
像这样的事情:
<ContentControl IsEnabled="False">
<Border>
<Button Content="Button" IsEnabled="True"/>
</Border>
</ContentControl>
所以在这个例子中,按钮的 IsEnabled="True" 设置被其父级覆盖。有没有办法阻止这种情况发生?
这似乎是一件奇怪的事情,但我遇到了一种情况,当一个控件被禁用并且用户将鼠标放在它上面时,我仍然希望触发一个事件。
我在 WPF Unleashed 中读到,将某些内容包装在 Frame 控件中,“..将内容与 UI [和] 属性的其余部分隔离,这些属性通常会在到达 Frame 时沿元素树停止继承”,但包装 Button在上面的例子中,在框架中不起作用。
我在这里走错路了吗?
Let's say I have some Control that has been disabled. It contains a bunch of elements, but I want one of those child elements to remain enabled.
Something like this:
<ContentControl IsEnabled="False">
<Border>
<Button Content="Button" IsEnabled="True"/>
</Border>
</ContentControl>
So in this example, the Button's IsEnabled="True" setting gets overridden by its parent. Is there a way of stopping this from happening?
This seems like a strange thing to be doing, but I have a situation where, when a Control is disabled and a user mouses down on it, I still want an event to be triggered.
I read in WPF Unleashed that wrapping something in a Frame control, "..isolates the content from the rest of the UI [and] properties that would normally be inherited down the element tree stop when they reach the Frame", but wrapping the Button in the example above in a Frame doesn't work.
Am I on the wrong track here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是继承,它是组合,
包含的控件被禁用,因此它包含的所有控件都被禁用
启用容器和所需的控件,并禁用其他控件
that's not inheritance, it's composition
the containing control is disabled, thus all of its contained controls are disabled
enable the container and the control that you want, and disable the other controls
如果您可以继承按钮(或您需要的任何控件),您可以替换 CoerceValue 以避免这种行为(事实上,我认为 IsEnabled 应该像 IsChecked 一样工作,是一个 bool?,其中 null 表示使用父值,任何应保留特定值...但这不是行为,使用此解决方案将解决问题):
If you can inherit the button (or any control that you need), you can replace the CoerceValue to avoid this behavior (in fact, I think IsEnabled should work like IsChecked, being a bool?, where null means use the parent value, any specific value should be kept... but as that's not the behavior, using this solution will solve the problem):