Saturday, 10 January 2009

Disable a toolbar button image in WPF

When I worked with winforms, it was very easy to disable a toolbar button image, just set Enabled=False and your were done. With WPF on the other hand, it is a little bit more complicated.

I love WPF! You can do everything you couldn’t to in winforms. You have total control. But the downside of having total control is that you must control everything. :-)

So, to disable a toolbar button with an image, which I do via application commands, use this following trigger.

<Style TargetType="{x:Type Image}" x:Key="toolbarImageStyle">
            <Style.Triggers>
                <DataTrigger 
Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}, 
AncestorLevel=1}, Path=IsEnabled}" Value="False">
                    <Setter Property="Opacity" Value="0.50"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>

And in the toolbar apply the style.

<Button 
 Name="btPlayersDelete" 
Command="commands:UserCommands.DeleteUsers" 
ToolTip="Delete players">
 
    <Image 
             Style="{StaticResource toolbarImageStyle}" 
             Name="imgPlayersDelete" 
Source="Images/Toolbar/delete.png"/> </Button>

Labels: ,

kick it on DotNetKicks.com

1 Comments:

Anonymous Mona said...

Exactly what I was looking for,

Thank you!

16 April 2010 16:06  

Post a Comment

Links to this post:

Create a Link

<< Home