WPF:从代码隐藏向元素添加阴影效果

发布于 2024-09-29 12:00:34 字数 35 浏览 0 评论 0原文

我以为这会很简单,但到目前为止我什么也没发现。你怎么做?

I thought this would be something simple but so far i found nothing. How do you do it?

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

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

发布评论

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

评论(3

り繁华旳梦境 2024-10-06 12:00:34

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

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

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

The accepted answer is now obsolete. Now you may use:

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

To achieve the exact same effect as the accepted answer.

诠释孤独 2024-10-06 12:00:34

试试这个

// 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; 

just try this

// 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; 
瑾兮 2024-10-06 12:00:34

@Gleno 的回答对我帮助最大。就我而言,我使用它来对错过的表单项进行视觉反馈。然后删除我使用的投影:

myComboBox.ClearValue(EffectProperty);

在 SelectionChanged 事件中。

希望这对某人有帮助。我不得不寻找一下。

@Gleno's answer helped me the most. In my case I was using it for visual feedback on a missed form item. To then remove the dropshadow I used:

myComboBox.ClearValue(EffectProperty);

in a selectionChanged event.

Hope this helps someone. I had to search for a bit.

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