Marta
Marta

Reputation: 2947

WPF ContentControl : how to change control opacity in disabled mode in .cs file?

I have public class MyControl : ContentControl in wchich i have definitions of control and events corresponding with it.

This control works fine, but when is disabled it looks still like enabled. I would like to do something like if control.isenabled = false then control.opacity = 0.5; How can i do it?

Upvotes: 0

Views: 4579

Answers (2)

Marta
Marta

Reputation: 2947

As you said - this trigger don't work - I have checked...

But I have found solution - in OnApplyTemplate method I have added few lines:

public override void OnApplyTemplate()
{
    //...
    if (this.IsEnabled == false)
    {
        this.Opacity = 0.4;
    }
}

Upvotes: 0

Yuriy Zanichkovskyy
Yuriy Zanichkovskyy

Reputation: 1699

In WPF for such things triggers are used.

<MyControl>
<MyControl.Triggers>
   <Trigger Property="IsEnabled" Value="false">
       <Setter Property="Opacity" Value="0.5" />
   </Trigger>
</MyControl.Triggers>
</MyControl>

Upvotes: 1

Related Questions