XAML .NET MAUI:覆盖窗口视图的默认样式(文本框)

bihw5rsg  于 2023-01-10  发布在  .NET
关注(0)|答案(1)|浏览(197)

我想创建一个具有完全个性化视觉效果的自定义条目
为此,我创建了一个CustomEntryHandler来修改windows平台的本地视图,但我无法覆盖导入某些效果的基本windows样式:

  • 随时间变化的背景色
  • 焦点位于条目时显示的下边框
  • ...

我想我理解了这种样式来自windows的默认样式,在通用的. xaml文件中。
有人知道我怎么能撤销这个吗?

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

    • 2022年11月10日更新**:我还想删除文本框的清除按钮。

先谢了。

tvokkenx

tvokkenx1#

最简单的方法是使用以下代码编写一个新的Style并将其应用于控件:

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

在App.xaml中,您需要添加以下样式:

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

您可以找到标准的WinUI 2 TextBox样式here。如果要删除清除按钮,可以在此处删除作为TextBoxStyle一部分的按钮。

相关问题