Wpf 为什么带有自定义控件模板的文本框没有可见的文本光标?
我有一个自定义控件模板,它是通过文本框上的style属性设置的。可视窗口设置正确,即使在文本框中键入也能正常工作,但没有可见的插入光标(符号),这使得编辑对我们的用户具有挑战性 控件模板需要如何更改才能恢复传统的文本框行为Wpf 为什么带有自定义控件模板的文本框没有可见的文本光标?,wpf,textbox,cursor,Wpf,Textbox,Cursor,我有一个自定义控件模板,它是通过文本框上的style属性设置的。可视窗口设置正确,即使在文本框中键入也能正常工作,但没有可见的插入光标(符号),这使得编辑对我们的用户具有挑战性 控件模板需要如何更改才能恢复传统的文本框行为 <Style x:Key="DemandEditStyle" TargetType="TextBox"> <EventSetter Event="LostFocus" Handler="DemandLostFocus" /> <S
<Style x:Key="DemandEditStyle" TargetType="TextBox">
<EventSetter Event="LostFocus" Handler="DemandLostFocus" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Stretch" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="1" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="1" />
</Grid.RowDefinitions>
<Grid.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="White" Offset="0" />
<GradientStop Color="White" Offset="0.15" />
<GradientStop Color="#EEE" Offset="1" />
</LinearGradientBrush>
</Grid.Background>
<Border Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Background="Black" />
<Border Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" Background="Black" />
<Grid Grid.Row="0" Grid.Column="0" Margin="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="1" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1" />
<RowDefinition Height="*" />
<RowDefinition Height="1" />
</Grid.RowDefinitions>
<Border Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" Background="Black" />
<Border Grid.Row="0" Grid.Column="0" Grid.RowSpan="3" Background="Black" />
<Border Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" Background="#CCC" />
<Border Grid.Row="0" Grid.Column="2" Grid.RowSpan="3" Background="#CCC" />
<TextBlock Grid.Row="1" Grid.Column="1"
TextAlignment="Right" HorizontalAlignment="Center" VerticalAlignment="Center"
Padding="3 0 3 0" Background="Yellow"
Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Text}"
Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}, AncestorLevel=1}, Path=ActualWidth}" />
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
更新:用ScrollViewer替换最里面的文本框并将其命名为PART\u ContentHost确实显示了文本插入光标。我想原因是模板缺少名为
PART\u ContentElement
的元素。如上所述,具有该名称的元素用于显示文本框的内容。但是,在中,该元素称为PART\u ContentHost
,并进一步限制为ScrollViewer
或AdorneDecorator
,您的样式应基于旧样式的文本框:
<Style x:Key="DemandEditStyle" TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
这对模板本身内部的问题没有帮助,因为不同样式之间没有ControlTemplates的合并。对于给定的元素,一次只存在一个元素。这就做到了:最里面的元素需要是一个ScrollViewer,正如建议的那样。谢谢!但是现在我似乎找不到一个位置来指示文本框中的文本应该右对齐,而不是左对齐。我尝试在ScrollViewer和样式上都将HorizontalContentAlignment设置为右侧,但没有效果。建议?由于默认模板不包含任何特殊对齐绑定,而仅在样式的Setter
中设置水平内容对齐
,我希望在您的样式中添加一个Setter
就足够了,它将水平内容对齐设置为右侧
,但显然您已经尝试过了。您的ScrollViewer
是否占用了所有可用空间,或者这可能会使文本看起来也是左对齐的吗?!找到答案:TextAlignment是要在样式中设置的属性,而不是HorizontalContentAlignment!由于宽度绑定,ScrollViewer被适当地拉伸。