Header 属性 在 UWP CustomTextBox 中不工作
Header property is not working in UWP CustomTextBox
在 UWP 中,我创建了派生自 TextBox 控件的 CustomTextBox。在我的 CustomTextBox 中,我使用了各种属性,但有些属性有效,有些无效。
以下属性工作正常,
- 宽度高度 BorderBrush 等。
以下属性无效,
- Header 文字等。请在下面找到代码片段,
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
原因是您覆盖了默认模板,因此删除了通常构成 TextBox
的所有元素。
要理解这一点,重要的是要明确 UWP 控件通常只提供 行为 ,但它们本身由模板中的多个其他控件组成(术语通常 XAML 控件默认为 look-less)。这些控件提供控件的可视化表示。例如,在 TextBox
的情况下,模板由 Border
用于其周围的边框,TextBlock
用于占位符文本,Button
用于清除内容,ScrollViewer
,其中呈现 Text
输入,ContentPresenter
呈现 header,依此类推。
您的自定义 TextBox
实际上只是 Border
本身,这意味着您基本上 失去了 您提到的功能,因为没有控件可以在视觉上他们。
要解决您的问题,我建议从默认的 TextBox
模板开始(通过将其复制并粘贴到您自己的样式中。然后只需编辑您想要更改。这样您将从功能齐全的状态开始,并可以在保留控件功能的同时决定要更改的内容。
您可以在 C:\Program Files (x86)\Windows Kits\DesignTime\CommonConfiguration\Neutral\UAP\{version}\Generic\generic.xaml
中找到 默认模板 。您可以搜索 <Style TargetType="TextBox">
以找到默认的 TextBox
样式和模板。
在 UWP 中,我创建了派生自 TextBox 控件的 CustomTextBox。在我的 CustomTextBox 中,我使用了各种属性,但有些属性有效,有些无效。
以下属性工作正常,
- 宽度高度 BorderBrush 等。
以下属性无效,
- Header 文字等。请在下面找到代码片段,
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
原因是您覆盖了默认模板,因此删除了通常构成 TextBox
的所有元素。
要理解这一点,重要的是要明确 UWP 控件通常只提供 行为 ,但它们本身由模板中的多个其他控件组成(术语通常 XAML 控件默认为 look-less)。这些控件提供控件的可视化表示。例如,在 TextBox
的情况下,模板由 Border
用于其周围的边框,TextBlock
用于占位符文本,Button
用于清除内容,ScrollViewer
,其中呈现 Text
输入,ContentPresenter
呈现 header,依此类推。
您的自定义 TextBox
实际上只是 Border
本身,这意味着您基本上 失去了 您提到的功能,因为没有控件可以在视觉上他们。
要解决您的问题,我建议从默认的 TextBox
模板开始(通过将其复制并粘贴到您自己的样式中。然后只需编辑您想要更改。这样您将从功能齐全的状态开始,并可以在保留控件功能的同时决定要更改的内容。
您可以在 C:\Program Files (x86)\Windows Kits\DesignTime\CommonConfiguration\Neutral\UAP\{version}\Generic\generic.xaml
中找到 默认模板 。您可以搜索 <Style TargetType="TextBox">
以找到默认的 TextBox
样式和模板。