WPF: programmatically changes the color of a control using a custom style

<DrawingImage x:Key="HexagonImage">
    <DrawingImage.Drawing>
        <DrawingGroup>
            <GeometryDrawing Brush="White"  
                       Geometry="M 250,0 L 750,0 L 1000,433 L 750,866 L 250,866 L 0,433 Z">
                <GeometryDrawing.Pen>
                    <Pen Brush="Black" Thickness="10" LineJoin="Round"/>
                </GeometryDrawing.Pen>
            </GeometryDrawing>
        </DrawingGroup>
    </DrawingImage.Drawing>
</DrawingImage>

<Style x:Key="HexagonButton" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
        <ControlTemplate TargetType="{x:Type Button}">
            <Grid>
                <Image x:Name="hexImg" Source="{StaticResource HexagonImage}"/>
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Grid>
        </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

I have a button that has this HexagonButton as its style, and I want to change its color programmatically, Iv'e tried to change the Backgroup property, but to no avail.

the only way I did this is to create a completely new style with a new drawing image. and assign it. But I'm sure there is an easier way to do this.

+5
source share
2 answers

, , GeomteryDrawing Button RelativeSource Foreground Background Button ( ):

<Style x:Key="HexagonButton" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="White" />
    <Setter Property="Foreground" Value="Black" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Image x:Name="hexImg">
                        <Image.Source>
                            <DrawingImage>
                                <DrawingImage.Drawing>
                                    <DrawingGroup>
                                        <GeometryDrawing Brush="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}}, Path=Background}" Geometry="M 250,0 L 750,0 L 1000,433 L 750,866 L 250,866 L 0,433 Z">
                                            <GeometryDrawing.Pen>
                                                <Pen Brush="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}}, Path=Foreground}" Thickness="10" LineJoin="Round" />
                                            </GeometryDrawing.Pen>
                                        </GeometryDrawing>
                                    </DrawingGroup>
                                </DrawingImage.Drawing>
                            </DrawingImage>
                        </Image.Source>
                    </Image>
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

:

<Button Style="{StaticResource HexagonButton}">Click me</Button>

:

<Button Style="{StaticResource HexagonButton}" Background="Yellow" Foreground="Red">Click me</Button>
+2

, Background="{TemplateBinding Background}" Grid ControlTemplate.

, Brush GeometryDrawing Transparent.

+1

All Articles