.NET MAUI : Override the default style of a windows view (TextBox)
Asked Answered
P

1

2

I want to create a custom Entry with a completely personalized visual.

For this, I created a CustomEntryHandler to modify the native view of the windows platform but I can't override the basic windows style which imports some effects :

  • The background color that changes on over
  • The bottom border that is displayed when entry is focused
  • ...

I think I understood that this style comes from the default style of windows, in the generic.xaml file.
Does anyone know how I can override this ?

        protected  override TextBox CreatePlatformView()
    {
        var nativeView = new TextBox();

        nativeView.Margin = new Microsoft.UI.Xaml.Thickness(0, 0, 0, 0);
        nativeView.FocusVisualMargin = new Microsoft.UI.Xaml.Thickness(0, 0, 0, 0);
        nativeView.BorderThickness = new Microsoft.UI.Xaml.Thickness(0,0,0,0);
        nativeView.Padding = new Microsoft.UI.Xaml.Thickness(0, 0, 0, 0);
        nativeView.CornerRadius = new Microsoft.UI.Xaml.CornerRadius(0);
        nativeView.Background = new SolidColorBrush(Colors.Transparent);

        return nativeView;
    }

Screenshot of the entry focused with code above


UPDATE 11/10/22 : I also want to remove the Clear button of the TextBox.

Thanks in advance.

Papandreou answered 8/10, 2022 at 15:5 Comment(4)
what do you mean by "visual effect on the mouse over" of a TextBox ?Fichtean
Hello @Cfun, I edited my post, I hope that answers your question. ThanksPapandreou
Is it OK to code in XAML?Muumuu
Yes, I tried without success.Papandreou
F
1

The easiest way would be to write a new Style and apply it to your control using this code:

var nativeView = new TextBox();
nativeView.Style = Application.Current.Resources["MyCustomTextBoxStyle"];

In your App.xaml, you need to add the following style:

<Style x:Key="MyCustomTextBoxStyle" TargetType="TextBox">
<!-- Your textbox style -->
</Style>

You can find the standard WinUI 2 TextBox style here. If you would like to remove the clear button, you could remove the button that is part of the TextBoxStyle here.

Fincher answered 6/1, 2023 at 13:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.