8

I'd like to know if it's possible to do the following:

  • create a black and white image, say of a dog
  • add the image to a button as a mask
  • change the style using (data) triggers, e.g. disabled - the dog is grey, "loading" - the dog is red, "ready" the dog is yellow, etc.

Basically I want to create button icons using pixels but be able to set the color at runtime depending on triggers. Something like this:

dog buttons

3 Answers 3

11

After 2 hours of googling here's the answer

<Window x:Class="IconTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <!-- button style -->
        <Style x:Key="ToolButton" TargetType="Button">
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border Width="16" Height="16" Background="#ffbbbbbb">
                            <Rectangle Name="rect" Fill="Black" OpacityMask="{TemplateBinding Content}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="rect" Property="Fill" Value="Green" />
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter TargetName="rect" Property="Fill" Value="Yellow" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </Window.Resources>
    <Grid>
        <Button Width="16" Height="16" Style="{StaticResource ToolButton}">
            <ImageBrush ImageSource="/test.png"/>
        </Button>
    </Grid>
</Window>
0
2

You can define an OpacityMask for the Button. I believe you will be able to add triggers (if you need them) to change the Background of Button.

enter image description here

Sample:

<Button Height="100" Width="100" Background="Green">
    <Button.OpacityMask>
    <DrawingBrush AlignmentX="Left" AlignmentY="Top">
        <DrawingBrush.Drawing>
        <DrawingGroup>
            <GeometryDrawing Brush="#33000000">
            <GeometryDrawing.Geometry>
                <RectangleGeometry Rect="0,0,40,40" />
            </GeometryDrawing.Geometry>
            </GeometryDrawing>
            <GeometryDrawing Brush="#FF000000">
            <GeometryDrawing.Geometry>
                <RectangleGeometry Rect="10,10,20,20">
                <RectangleGeometry.Transform>
                    <RotateTransform Angle="45" CenterX="20" CenterY="20" />
                </RectangleGeometry.Transform>
                </RectangleGeometry>
            </GeometryDrawing.Geometry>
            </GeometryDrawing>
        </DrawingGroup>
        </DrawingBrush.Drawing>
    </DrawingBrush>
    </Button.OpacityMask>
</Button>
0

In my opinion, you don't need create image mask in WPF to achieve your requirement. You could simply edit the control template of the button class.

Please refer to the Ellipse button example in this page. Control Template

Then you could simply change the Fill Color of that shape using trigger. Trigger

Cheers,

1
  • I need to use an image to set the icon since I'm needing more than geometric shapes.
    – DaveO
    Commented Apr 26, 2011 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.