2

I want to change the ellipse fill color to green when the button is enabled.

   <Button x:Name="btn_Kaart2" Grid.Column="1" Grid.Row="2" IsEnabled="False">
        <Button.Template>
            <ControlTemplate>
                <Ellipse x:Name="ellipse_2" 
                         Height="35"
                         Stroke="Black"
                         Fill="Red"
                         Margin="-300,440,0,0"/>
            </ControlTemplate>
        </Button.Template>
    </Button>

Normally I would use ellipse_2.Fill = "Color" but that doesn't work, program can't find ellipse_2

UkoM
  • 305
  • 2
  • 6
  • 17

1 Answers1

4

You can use a databinding with RelativeSource to get this without any additional code

<Button x:Name="btn_Kaart2" Grid.Column="1" Grid.Row="2" IsEnabled="False">
    <Button.Template>
        <ControlTemplate>
            <Ellipse x:Name="ellipse_2" 
                Height="35"
                Margin="-300,440,0,0"
                Stroke="Black">
            <Ellipse.Style>
                <Style TargetType="Ellipse">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsEnabled, RelativeSource={RelativeSource AncestorType=Button}}" Value="false">
                            <Setter Property="Fill" Value="red"></Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding IsEnabled, RelativeSource={RelativeSource AncestorType=Button}}" Value="true">
                            <Setter Property="Fill" Value="green"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Ellipse.Style>
            </Ellipse>
        </ControlTemplate>
    </Button.Template>
</Button>

It's important that the Ellipse itself has no Fill attribute.
You can try this by changing the IsEnabled value in the *.xaml and the color should change immediately in the designer.

AntiHeadshot
  • 1,130
  • 9
  • 24
  • Well, that should work, but for some reason it won't. I'll mark this as answered and try to implement the button in a other way. – UkoM Dec 17 '15 at 15:49
  • My bad again, forgot to remove the Fill property. Thanks! – UkoM Dec 17 '15 at 15:51