标头属性在UWP CustomTextBox中不起作用

时间:2019-10-31 05:43:17

标签: c# uwp uwp-xaml

在UWP中,我创建了从TextBox控件派生的CustomTextBox。在我的CustomTextBox中,我使用了各种属性,但是某些属性有效,而有些则无效。

以下属性运行正常,

  • 宽度,高度,边框刷等。

以下属性不起作用

  • 标题文本等。请在下面找到代码段,

MyTextBox.cs

public sealed class MyTextBox : TextBox
{
    public MyTextBox()
    {
        this.DefaultStyleKey = typeof(MyTextBox);
    }
}

Generic.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:SfMaskedEdit_header">

    <Style TargetType="local:MyTextBox" >
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:MyTextBox">
                    <Border
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

MainPage.xaml

<Page
    x:Class="SfMaskedEdit_header.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:SfMaskedEdit_header"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid x:Name="grid1">

        <local:MyTextBox x:Name="myTextBox" Text="Hello" Header="MyTextBox" Width="200" Height="40" 
                         BorderBrush="Blue" BorderThickness="2" Background="Pink"/>

    </Grid>
</Page>

请使用以下示例获取更多信息,

示例: MyTextBox

1 个答案:

答案 0 :(得分:0)

原因是您已覆盖默认模板,因此删除了通常构成TextBox的所有元素。

要理解这一点,重要的是要弄清楚UWP控件通常只是提供一个行为,但它们本身由其模板中的多个其他控件组成(术语通常是XAML控件默认为 look-less )。这些控件提供了控件的可视化表示。例如,对于Border,模板由TextBlock组成,用于边框,Button占位符文本,ScrollViewer用于清除内容,一个Text,其中显示ContentPresenter输入,一个TextBox表示标题,依此类推。

您的自定义Border实际上只是TextBox本身,这意味着您基本上已经丢失了您提到的功能,因为没有控制 surface < / em>视觉上看。

为解决您的问题,建议从默认的C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\{version}\Generic\generic.xaml模板开始(通过将其复制并粘贴到您自己的样式中,然后编辑要更改的部分)。这样,您将从完整的功能状态开始,可以决定要进行哪些更改,同时保留控件的功能。

您可以在<Style TargetType="TextBox">中找到默认模板。您可以搜索TextBox来找到默认的Caused by: java.lang.NullPointerException at java.net.InetAddress$Cache.put(InetAddress.java:806) at java.net.InetAddress.cacheAddresses(InetAddress.java:885) at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1362) at java.net.InetAddress.getAllByName0(InetAddress.java:1276) at java.net.InetAddress.getAllByName(InetAddress.java:1192) at java.net.InetAddress.getAllByName(InetAddress.java:1126) at java.net.InetAddress.getByName(InetAddress.java:1076) at java.net.InetSocketAddress.<init>(InetSocketAddress.java:220) at sun.net.NetworkClient.doConnect(NetworkClient.java:175) at sun.net.www.http.HttpClient.openServer(HttpClient.java:451) at sun.net.www.http.HttpClient.openServer(HttpClient.java:546) at sun.net.www.http.HttpClient.<init>(HttpClient.java:214) at sun.net.www.http.HttpClient.New(HttpClient.java:326) at sun.net.www.http.HttpClient.New(HttpClient.java:345) ........... ........... 样式和模板。