Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Style.Triggers TargetType="Button" Property="IsEnabled"

$
0
0

Hello,

I wanted to give a new try to Triggers to avoid using converters / VisualState / CustomRenderer.

I got a ListView filled with data which contains several buttons. Those buttons have their IsEnabled property binded and they use a style which use a Trigger to change the TextColor / Opacity / BackgroundColor to show to user that a button is either available or not.

It doesn't seems to work as I expect.

Here is the style :
<Style x:Key="ContextualCellButtonStyle" TargetType="Button">
<Style.Triggers>
<Trigger TargetType="Button" Property="IsEnabled" Value="False">
<Setter Property="Text" Value="IsEnabled=False" />
<Setter Property="TextColor" Value="Green" />
<Setter Property="BackgroundColor" Value="Red" />
</Trigger>
<Trigger TargetType="Button" Property="IsEnabled" Value="True">
<Setter Property="Text" Value="IsEnabled=True" />
<Setter Property="TextColor" Value="Red" />
<Setter Property="BackgroundColor" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>

(The values used are not the final one, it's just to debug)
As you can see, when a button is enabled, the text should be IsEnabled=True with a green background and red foreground.
If the button is not enabled, the text should be IsEnabled=False with a red background and green foreground.

In my design time model, I've set the first item to enabled and the second one to disabled :

And in my XAML, I have this :

Here is the result I got :

You can see that when the IsEnabled=False, it uses the trigger IsEnabled=True and when the IsEnabled is set to true, I don't have the style I want too.

I guess the Triggers work when the IsEnabled property changes at runtime but I'm not sure. If it is the case, what do you recommend ? A custom control ? A custom renderer ?
I would like to avoid VisualState because I'd like to use MVVM without code behind for this stuff.

I have already written some custom renderer like this but I thought Trigger would be better in this case so I won't have to create a custom control and a custom renderer :

Any help or suggestion is welcomed.
Thanks for reading so far !


Viewing all articles
Browse latest Browse all 77050

Trending Articles