wpf DropShadowEffect 到 XAML 中的特定元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13374010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DropShadowEffect to a specific element in XAML
提问by KMC
I have a Borderthat consist of other UI Controls like Canvas, Buttons, ComboBox etc. I only want a DropShadowEffecton the Border, but all children Controls end up inheriting the DropShadowEffect.
我有一个由其他 UI 控件(如画布、按钮、组合框等)组成的边框。我只想要边框上的DropShadowEffect,但所有子控件最终都继承了DropShadowEffect。
For example, the following code produce DropShadowEffecton the TextBox, Button, ComboBox. How to I apply DropShadowEffectONLY to the border?
例如,以下代码在 TextBox、Button、ComboBox 上生成DropShadowEffect。如何仅将DropShadowEffect应用于边框?
<Border>
<Border.Effect>
<DropShadowEffect ...>
</Border.Effect>
<Canvas>
<TextBox>...</TextBox>
<Button>...</Button>
<ComboBox>...<ComboBox>
</Canvas>
</Border>
回答by Rohit Vats
When a DropShadowEffectis applied to a layout container, such as DockPanel or Canvas, the effect is applied to the visual tree of the element or visual, including all of its child elements.
当 aDropShadowEffect应用于 a 时layout container,例如 DockPanel 或 Canvas,效果将应用于元素或视觉对象的可视化树,包括其所有子元素。
But following article shows a workaround to achieve this goal here.
但是下面的文章显示了在此处实现此目标的解决方法。
Let's say you have the Border with effect. Just have another border with same position but without the effect, that would resolve the problem -
假设您有 Border 效果。只要有another border with same position but without the effect,就可以解决问题-
<Border Margin="10">
<Border.Effect>
<DropShadowEffect ...>
</Border.Effect>
</Border>
<Border Margin="10">
<Canvas>
<TextBox>...</TextBox>
<Button>...</Button>
<ComboBox>...<ComboBox>
</Canvas>
</Border>
回答by D J
Bit tricky, I did it like this.
有点棘手,我是这样做的。
<ControlTemplate x:Key="ShadowBorderShadowTemplate">
<!-- Start shadow effect to fragment -->
<Grid x:Name="Transform">
<Border BorderThickness="1"
BorderBrush="Gray"
Background="{x:Null}"
Margin="1">
<Border.Effect>
<DropShadowEffect BlurRadius="6"
Direction="270"
ShadowDepth="2" />
</Border.Effect>
</Border>
<Border BorderThickness="0"
Margin="1,2,1,1"
BorderBrush="{x:Null}"
Background="White" />
</Grid>
<!-- End shadow effect to fragment -->
</ControlTemplate>
<ControlTemplate x:Key="ContentControlTemplateWithShadow"
TargetType="{x:Type ContentControl}">
<Grid>
<!-- Shadow around the left nav -->
<ContentControl Template="{DynamicResource ShadowBorderShadowTemplate}" />
<ContentPresenter />
</Grid>
</ControlTemplate>
and use the resources like
并使用资源,如
<ContentControl Template="{StaticResource ContentControlTemplateWithShadow}">
<Border>
<Canvas>
<TextBox Text="ABCD" Canvas.Left="115" Canvas.Top="134" />
<Button Canvas.Left="115" Canvas.Top="91">Test</Button>
<ComboBox Canvas.Left="115" Canvas.Top="54" />
</Canvas>
</Border>
</ContentControl>
Hope it helps..
希望能帮助到你..

