Search code examples
textboxruntimewinui-3winui

How to eliminate the WinUI3 TextBox underscore at runtime?


I've been scratching my head over what should be a very simple problem. I've been developing GUIs for decades, but I'm something of a tyro with WinUI 3. I'm developing a Maui custom Entry (WinUI TextBox) control, and have access at runtime to the native control. It displays a rather annoying underscore character I want to get rid of since I'm implementing my own border; I want to either eliminate it programmatically, or render it invisible by setting its color to Transparent. I cannot, for the life of me, figure out how to do this simple task. Can anyone suggest an appropriate path? I've been Googling and Binging everywhere and even ChatGPT doesn't offer a solution.

I have no idea what to try at this point. I see no way to influence the underscore without also changing the text.


Solution

  • The line at the bottom border of TextBox is a Border named "BorderElement".

    In order to access this element inside TextBox, you can use the VisualTreeHelper but it's easier using the CommunityToolkit.WinUI.UI NuGet package.

    private void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        if (this.TextBoxControl
            .FindDescendants()
            .OfType<Border>()
            .FirstOrDefault(x => x.Name is "BorderElement") is Border borderElement)
        {
            borderElement.Visibility = Visibility.Collapsed;
        }
    }
    

    You can see how this "BorderElement" is in the "DefaultTextBoxStyle" in the generic.xaml.