我正在 wpf 窗体 (从 winform 搬几天前),和我想要自定义我的文本框。我得到了文本框中表现得如何,我想要它,但现在我不能给它输入,和它根本没有响应单击它时。我想我把它弄坏了,反正这里是我的代码︰
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="10,48,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" Foreground="White">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="BorderBrush" Value="#FF497AB4"/>
<Setter Property="Background" Value="#FF2E2E2E"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="OrangeRed"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
所以我到底做错了?谢谢你 !
请尝试将 Scrollviewer 添加到您的模板,像这样︰
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
</Border>
你有一些示例模板在这里
问题是模板没有 ContentHost,所以它不会呈现的内容。若要添加 ContentHost,你应该添加名为"PART_ContentHost"作为解释这里的元素