How do I programatically set a FlyoutItemLabel Text Color

Mark Rooney 21 Reputation points
2021-04-27T23:38:48.557+00:00

Here's the scenario: I am building a mobile application that will adapt its color theme based on the customer, where their logo and theme colors are stored in a database. I retrieve those values and set them in my ViewModel. Now I'm trying to retrieve them in the UI. In My AppShellViewModel I have the Background Property, and run the following snippet:

<VisualState.Setters>
<Setter TargetName="FlyoutItemLabel" Property="Label.TextColor" Value="{Binding Background}" />
</VisualState.Setters>

No matter what I try, the font is always black (instead of what it should be).

So my question is, is there a way in the code behind to set the property for the FlyoutItemLabel TextColor in the code behind, since it appears that customizing through the XAML isn't working/binding? Thanks!

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. CherryBu-MSFT 326 Reputation points
    2021-04-28T03:27:58.273+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    So my question is, is there a way in the code behind to set the property for the FlyoutItemLabel TextColor in the code behind, since it appears that customizing through the XAML isn't working/binding? Thanks!

    You can use DynamicResource to change FlyoutItemLabel TextColor by code behind.

    Firstly, you need to create Color resource in App.xaml.

     <Application.Resources>  
            <ResourceDictionary>  
                <Color x:Key="Primary">#2196F3</Color>  
              
            </ResourceDictionary>  
        </Application.Resources>  
    

    Then using this resource by style in Shell.Resource.

     <Style Class="FlyoutItemLabelStyle" TargetType="Label">  
                    <Setter Property="TextColor" Value="{DynamicResource Primary}" />  
                </Style>  
    

    You can change this resource color by code behind:

     private void btn_Clicked(object sender, EventArgs e)  
            {  
                Application.Current.Resources["Primary"] = Color.Red;  
            }  
    

    Best Regards,

    Cherry Bu

    ---
    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.