Home > OS >  TextBox not giving up Cursor Focus - WinUI
TextBox not giving up Cursor Focus - WinUI

Time:10-14

I have the following textbox that I want to essentially set to disabled without having the Style change to the hard to read grey-on-grey look that it would have if I did IsEnabled=False. I seem to have gotten it working with the following xaml:

<TextBox Text="{x:Bind Value, Mode=TwoWay}"
         IsHitTestVisible="{x:Bind IsEditable, Mode=OneWay}"
         IsFocusEngagementEnabled="{x:Bind IsEditable, Mode=OneWay}"
         IsTabStop="{x:Bind IsEditable, Mode=OneWay}"/>

My only problem happens when I temporarily enable access to the textbox when a user has clicked the edit button (setting IsEditable to True). The glitch is that the textbox maintains cursor focus if the user exits edit mode while clicked onto the textbox. Shouldn't the textbox lose focus when IsFocusEngagementEnabled goes back to being False?

Alternatively, is there any way I can just override the style of a disabled textbox in WinUI 3? If that is an option, it would probably be preferable as I could just bind IsEnabled to my variable and then programmatically toggle styles in the IsEnabledChanged EventHandler.

Thanks for your help.

CodePudding user response:

You can override the color like this.

<StackPanel>
    <StackPanel.Resources>
        <StaticResource
            x:Key="TextControlForegroundDisabled"
            ResourceKey="TextFillColorPrimaryBrush" />
    </StackPanel.Resources>
    <TextBox
        x:Name="TextBoxControl"
        Text="Default" />
</StackPanel>
  • Related