Reputation: 1306
I have two buttons within a Toolbar and I would like the button's opacity to change, if the Button isEnabled=false
I define the Style and a trigger in the Resources of my Window:
<Window.Resources>
<Style x:Key="buttonOpacity" TargetType="Button">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value="0.3" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
And here the Style should be applied:
<ToolBar>
<Button Click="FirstClick"
IsEnabled="False">
<Image Height="24" Source="Ressources/First.png"/>
</Button>
<Button Click="SecondClick"
IsEnabled="False"
Style="{StaticResource buttonOpacity}">
<Image Height="24" Source="Ressources/Second.png"/>
</Button>
</ToolBar>
I noticed, that the first Button doesn't apply the style and therefore doesn't change the oppacity. Whereas the second Button applies the style I gave to it. So, why does my first button not find the Resource? TargetType is set... Search goes upwards the logical tree...
Later on, I tried something similar with a Button within a DataGridTemplateColumn:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="thridButton"
IsEnabled="false"/>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
On the thridButton, even setting the style manually doesn't help. I'm pretty much lost... Can anyone see my mistake?
By the way: The IsEnabled Flag is set with Data Binding and a converter. since the Style works when the style is set manually, I don't think that the problem is there...
Upvotes: 0
Views: 442
Reputation: 18580
This because you have given the explicit x:key
to your Style
. So in order to apply the style to have to explicitly apply it. Just remove the x:Key
and it will be applied to all the buttons in your Window
.
Also you have button in toolbar. Toolbar applies its own style on buttons thats why they are flat by default. So in order to override the default style we will need to define style using the reserved key below:
<Style x:Key="{x:Static ToolBar.ButtonStyleKey}" TargetType="Button">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value="0.2" />
</Trigger>
</Style.Triggers>
</Style>
Note here {x:Static ToolBar.ButtonStyleKey}
is the key toolbar uses for its button style.
Upvotes: 1
Reputation: 6547
Your resource style has a key. Once you set up an explicit key, youv'e overriding it's implicit key and you need to state that key whenever you want to use it.
Just remove x:Key="buttonOpacity"
from your style to make it apply implicitly, or add the use of that key to make it apply explicitly
<ToolBar>
<Button Click="FirstClick"
Style="{StaticResource buttonOpacity}"
IsEnabled="False">
<Image Height="24" Source="Ressources/First.png"/>
</Button>
<Button Click="SecondClick"
IsEnabled="False"
Style="{StaticResource buttonOpacity}">
<Image Height="24" Source="Ressources/Second.png"/>
</Button>
</ToolBar>
Upvotes: 0