I am making Image/Text button. When mouse over event arised, I want to change color of my drawing brush. How can I this? In below example, I want to iconBrush's color, Red -> Blue when mouse over.
<SolidColorBrush x:Key="iconBrush" Color="Red"/>
<DrawingBrush x:Key="buttonIcon" Stretch="Uniform">
<DrawingBrush.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing Brush="{StaticResource iconBrush}" Geometry="... />
</DrawingGroup.Children>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
<Style x:Key="ImageTextButtonStyle" TargetType="{x:Type Button}">
...
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="10"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Background="{StaticResource buttonIcon}" />
<Grid Grid.Row="1">
<ContentPresenter ...
/>
</Grid>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="{StaticResource iconBrush}" Value="Blue" />
---> I Want to do this way..
This can be achieved quite simply using Attached Properties. You could also override the Button class but I prefer the first solution since you are only templating an existing control. Furthermore, you could reuse the attached property with controls other than Button.
The attached property:
The XAML part:
it is not possible directly the way you are trying to achieve as nested properties or setting the resources values are not supported in setters
here is a workaround
here is the code
Another approach
code
Attached Property for Icon
class to declare the property and hold the value
XAML resources
usage