Changing Visual state of an Entry from code behind

Manickam, Suraj 320 Reputation points
2023-11-15T13:27:13.34+00:00

0

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?

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

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 42,751 Reputation points Microsoft Vendor
    2023-11-16T02:58:37.3166667+00:00

    Hello,

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

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

    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

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.