views:

303

answers:

2

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?

A: 

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>
Yurec
Thanks, but i would like to make those changes in .cs file not in xaml. Can i set those triggers somehow in .cs file where i have definition of my coontrol?
Cassandra
Sure you can, but you shouldn't - it will be too complicated. Handle IsEnabledChanged event to change control opacity
Yurec
You actually can't use a Trigger like this. Property Triggers are only allowed in Style or Template Triggers collections. In this context only an EventTrigger would be valid. That said, Yurec is correct that it is much preferred in WPF to do this in the ControlTemplate XAML for your control than in code.
John Bowen
you are right, sorry my mistake
Yurec
A: 

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;
    }
}
Cassandra