Does the button using ICommand not turn off?

I have a control button in my wpf-mvvm application.

I use the ICommand property (defined in the viewmodel) to associate a button click event with the viewmodel.

I have → execute and canexecute execute parameters for my implementation of ICommand ( RelayCommand ).

Even if CanExecute is false ... the button is not disabled ... WHEN the CONTENT button is a PICTURE

But, when the contents of the button are text .. enable / disable works fine.

 <Button DockPanel.Dock="Top" Command="{Binding Path=MoveUpCommand}"> <Button.Content> <Image Source="/Resources/MoveUpArrow.png"></Image> </Button.Content> <Style> <Style.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Opacity" Value=".5" /> </Trigger> </Style.Triggers> </Style> </Button> 
+2
wpf mvvm icommand
Dec 26 '10 at 6:10
source share
3 answers

The button is disabled, it just does not affect the rendering of the image. You will need to write a trigger in the style that changes the opacity of the image to .5, and you get the desired effect of clicking the button as follows:

 <Style x:Key="imageButton" TargetType="Button"> <Style.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Opacity" Value=".5" /> </Trigger> </Style.Triggers> </Style> 
+14
Dec 26 '10 at 7:24
source share

Thank! I tried the suggested code after all my buttons. Does not work. I tried to extract only the trigger and insert it into the general button, in which all the other inherited buttons: worked like a charm! First this code:

 <Style x:Key="SecButton" TargetType="Button"> <Setter Property="FontSize" Value="16" /> <Setter Property="Margin" Value="0,0,5,5" /> <Style.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Opacity" Value=".5" /> </Trigger> </Style.Triggers> </Style> 

Based on the code above, I created these buttons:

 <Style x:Key="NewBtnStyle" TargetType="Button" BasedOn="{StaticResource SecButton}"> <Setter Property="ContentTemplate"> <Setter.Value> <DataTemplate> <StackPanel Orientation="Horizontal"> <Image Source="Images/new.png" Width="50" Height="50" /> </StackPanel> </DataTemplate> </Setter.Value> </Setter> </Style> 

When the buttons where disabled, the images in them are automatically darkened to 0.5 opacity.

+1
Jun 20 2018-12-12T00:
source share

You will need a pixel shader effect (it’s not as complicated as it seems, it just adds a link to the assembly, and then you can use it as easily as any built-in WPF effect) in combination with a trigger similar to what Hassan Khan sent, to show disabled button images in grayscale.

0
Dec 26 '10 at 8:50
source share



All Articles