WPF:为代码隐藏中的元素添加一个drophadow效果

时间:2010-10-26 10:23:15

标签: c# wpf .net-4.0

我认为这会很简单但到目前为止我什么都没发现。你是怎么做到的?

3 个答案:

答案 0 :(得分:48)

接受的答案现在已经过时了。现在你可以使用:

UIElement uie = ...
uie.Effect = 
    new DropShadowEffect
    {
        Color = new Color {A = 255, R = 255, G = 255, B = 0},
        Direction = 320,
        ShadowDepth = 0,
        Opacity = 1
    };

达到与接受的答案完全相同的效果。

答案 1 :(得分:8)

试试这个

// Get a reference to the Button.
Button myButton = new Button();

// Initialize a new DropShadowBitmapEffect that will be applied
// to the Button.
DropShadowBitmapEffect myDropShadowEffect  = new DropShadowBitmapEffect();
// Set the color of the shadow to Black.
Color myShadowColor = new Color();
myShadowColor.ScA = 1;
myShadowColor.ScB  = 0;
myShadowColor.ScG  = 0;
myShadowColor.ScR  = 0;
myDropShadowEffect.Color = myShadowColor;

// Set the direction of where the shadow is cast to 320 degrees.
myDropShadowEffect.Direction = 320; 

// Set the depth of the shadow being cast.
myDropShadowEffect.ShadowDepth = 25; 

// Set the shadow softness to the maximum (range of 0-1).
myDropShadowEffect.Softness = 1;
// Set the shadow opacity to half opaque or in other words - half transparent.
// The range is 0-1.
myDropShadowEffect.Opacity = 0.5; 

// Apply the bitmap effect to the Button.
myButton.BitmapEffect = myDropShadowEffect; 

答案 2 :(得分:7)

@Gleno的答案对我帮助最大。在我的情况下,我使用它来对错过的表单项进行视觉反馈。然后去掉我用过的阴影:

myComboBox.ClearValue(EffectProperty);

在selectionChanged事件中。

希望这有助于某人。我不得不搜索一下。

相关问题