Search code examples
xamarin.formscolorsexpander

Xamarin Forms: How to change background color and textcolor when Expander is on or off?


My Expander Header code:

<Expander.Header>
    <Frame 
        BackgroundColor="White"">

        <StackLayout>

            <Label 
                TextColor="Black"/>

            <Image />
        </StackLayout>
    </Frame>
</Expander.Header>

I need to change the background color(to blue) of the frame and text color(to white) of the label when the expander is on. When the expander is collapsing I need to revert to the initial colors. Using DataTrigger and IsExpanded properties I am able to change the image source, but don't know how to change the colors.


Solution

  • Soemthing like:

            <Frame BackgroundColor="White">
                <Frame.Triggers>
                    <DataTrigger
                        Binding="{Binding Source={RelativeSource AncestorType={x:Type Expander}}, Path=IsExpanded}"
                        TargetType="Frame"
                        Value="True">
                        <Setter Property="BackgroundColor" Value="Black" />
                    </DataTrigger>
                </Frame.Triggers>
    
                <StackLayout>
    
                    <Label TextColor="Black">
                        <Label.Triggers>
                            <DataTrigger
                                Binding="{Binding Source={RelativeSource AncestorType={x:Type Expander}}, Path=IsExpanded}"
                                TargetType="Label"
                                Value="True">
                                <Setter Property="TextColor" Value="White" />
                            </DataTrigger>
                        </Label.Triggers>
                    </Label>
    
                    <Image />
                </StackLayout>
            </Frame>
    

    Or you can also bind to another control's property, please check the docs.