Changing Visual state of an Entry from code behind

Manickam, Suraj 40 Reputation points


I have been trying to change Visual State of an Entry from CodeBehind. so if the entry is disabled , background color should be changed to Grey and if its normal , it should be white which is clearly defined in styles.xaml ,Based on User input, I make a call VisualStateManager.GoToState(Entry, "Disabled");or VisualStateManager.GoToState(Entry, "Normal"). Once It is disabled where the background color changes to Grey , I could not change the background color anymore even when I forcefully set it or when I call the VisualStateManager. Am I missing something?

A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,148 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 24,591 Reputation points Microsoft Vendor


    After investigating, you need to add VisualStateGroupList to XAML in order to call GotoState(entry, "Disabled") normally.

            <VisualStateGroup x:Name="CommonStates">
                <VisualState x:Name="Normal">
                        <Setter Property="BackgroundColor" Value="Lime" />
                <VisualState x:Name="Focused">
                        <Setter Property="FontSize" Value="36" />
                <VisualState x:Name="Disabled">
                        <Setter Property="BackgroundColor" Value="Pink" />
                <VisualState x:Name="PointerOver">
                        <Setter Property="BackgroundColor" Value="LightBlue" />

    In addition, in .net 7 the GoToState method fails to trigger the Disabled state. Whereas in .net 6 and 8 this issue does not occur.

    Best Regards,

    Alec Liu.

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.

0 additional answers

Sort by: Most helpful