XAML:获取父背景 [英] XAML: Get parent background
问题描述
我需要在 XAML 中将控件的背景设置为父级背景的颜色.为什么不简单地使背景透明?这是一个带有阴影的按钮,所以我需要设置背景;否则,阴影会显示出来.
I need to set a control's background to the color of the parent's background in XAML. Why not simply make the background transparent? It's a button with a drop shadow, so I need to set the background; otherwise, the drop shadow shows through.
那么,从我的控件标记来看,我如何将 Background 属性设置为与父(主机)背景相同?感谢您的帮助.
So, from my control's markup, how do I set the Background property equal to whatever the parent (host) Background is? Thanks for your help.
推荐答案
您应该能够使用以下方法设置绑定:
You should be able to set the binding using:
<Button Background="{Binding Path=Background, RelativeSource={RelativeSource Mode="FindAncestor" AncestorType="{x:Type Control}" AncestorLevel="1"}}" />
因为背景是为任何控件"定义的,所以这应该抓取树上一个祖先的控件,并使用它的背景.
Since Background is defined for any "Control", this should grab the control one ancestor up the tree, and use it's background.
另一个需要考虑的选择是制作一个按钮样式,将背景显示为透明,但实际上仍然绘制阴影/边框.这将允许它在任何 UIElement 上工作.
Another option to consider would be to just make a button style that shows the background as transparent, but actually still draws the drop shadow/border. This would allow it to work on any UIElement.
这篇关于XAML:获取父背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!