WPF: добавить эффект drophadow к элементу из кода - PullRequest
12 голосов
/ 26 октября 2010

Я думал, что это будет что-то простое, но пока ничего не нашел.Как ты это делаешь?

Ответы [ 3 ]

48 голосов
/ 14 сентября 2011

Принятый ответ устарел. Теперь вы можете использовать:

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

Для достижения того же эффекта, что и принятый ответ.

8 голосов
/ 26 октября 2010

просто попробуйте это

// 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; 
7 голосов
/ 21 сентября 2015

@ Ответ Глено помог мне больше всего.В моем случае я использовал его для визуальной обратной связи по пропущенному элементу формы.Чтобы затем удалить тень, я использовал:

myComboBox.ClearValue(EffectProperty);

в событии selectionChanged.

Надеюсь, это кому-нибудь поможетМне пришлось немного поискать.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...