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

  • Question

  • User351573 posted

    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.

    Wednesday, November 18, 2020 9:01 AM

Answers

  • User351573 posted

    Got solution :)

        <Frame BackgroundColor="White">
            <Frame.Triggers>
                <DataTrigger
                    Binding="{Binding Source={RelativeSource AncestorType={x:Type Expander}}, Path=IsExpanded}"
                    TargetType="Frame"
                    Value="True">
                    <Setter Property="BackgroundColor" Value="Blue" />
                </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>
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, November 18, 2020 10:03 AM