1

I have WPF triangle button which is made using Polygon-geometry:

<UserControl x:Class="Formats.Triangle_Down"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:Formats"
         mc:Ignorable="d" Height="100" Width="100">
<Grid Margin="0,0,10,10">
    <Grid.RowDefinitions>
        <RowDefinition Height="7*"/>
        <RowDefinition Height="11*"/>
    </Grid.RowDefinitions>
    <Button Background="Transparent" Content="Button" Foreground="Transparent" Margin="0,0,50,0">
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <Grid Background="{TemplateBinding Background}">
                    <Polygon x:Name="triangle" Points="10,10 20,30 30,10"  Stroke="Lime"
                     StrokeThickness="2" Fill="Lime" Margin="0,0,-91,-85">
                        </Polygon>
                    </Grid>
        </Button.Template>
    </Button>
</Grid>

So my question is: how can I change Background color and Borderbrush of my Button using C# code? When I use this:

button.Background = System.Windows.Media.Brushes.Red;

nothing changes (if I have not mistaken, because it doesn't change filling of polygon). I know the way to change color using "border"

<Border Background="{TemplateBinding Background}">
<Button.Content>
</Border>

But it doesn't suit me 'cause form of my button is triangle.

So, how can I solve this problem?

1

1 Answer 1

0

In your template you put your polygon inside grid and bind that grid background to Button.Background, while hardcoding color of your polygon to Lime. That way you cannot change color of polygon later. Instead - bind polygon colors to Button.Background:

<Button Background="Lime"
        Content="Button"
        Foreground="Transparent"
        Margin="0,0,50,0">
    <Button.Template>
        <ControlTemplate TargetType="Button">
            <Polygon x:Name="triangle"
                     Points="10,10 20,30 30,10"
                     Stroke="{TemplateBinding Background}"
                     StrokeThickness="2"
                     Fill="{TemplateBinding Background}"
                     Margin="0,0,-91,-85"></Polygon>
        </ControlTemplate>
    </Button.Template>
</Button>

Then changing Button.Background will change colors of polygon.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.