question

MarkRooney-5865 avatar image
0 Votes"
MarkRooney-5865 asked MarkRooney-5865 commented

How do I programatically set a FlyoutItemLabel Text Color

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!

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

CherryBu-MSFT avatar image
1 Vote"
CherryBu-MSFT answered MarkRooney-5865 commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks! This is exactly what I was trying to figure out! It behaves (resources) like an extension that can be modified.

0 Votes 0 ·