我有一个"geric "styleTextBox与水印,它的工作,但是,当我请求一个焦点为这个文本框的命令行(textbox. Focus();)他有一个背景黄色,但光标键盘不显示,但我可以改变文本值,我做错了什么不显示这个光标?
<Style x:Key="StyledTextBox" TargetType="{x:Type TextBox}" BasedOn="{StaticResource baseStyle}">
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border x:Name="bg" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
FocusManager.FocusedElement="{Binding ElementName=inputText}">
<Grid>
<TextBlock Text="{TemplateBinding Tag}" VerticalAlignment="Center" Margin="8,0,0,0">
<TextBlock.Visibility>
<MultiBinding Converter="{StaticResource InputToVisibility}">
<Binding ElementName="inputText" Path="Text.IsEmpty" />
<Binding ElementName="inputText" Path="IsFocused" />
</MultiBinding>
</TextBlock.Visibility>
</TextBlock>
<TextBox Name="inputText" Background="Transparent" BorderThickness="0"
Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Text, UpdateSourceTrigger=PropertyChanged}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property=" IsFocused" Value=" true">
<Setter TargetName="bg" Property="BorderThickness" Value="5" />
<Setter TargetName="bg" Property="Background" Value="Yellow" />
</Trigger>
<Trigger Property="IsFocused" Value="false">
<Setter Property="Background" TargetName="bg" Value="red" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
你的问题的根源是你把一个文本框在你的TextBox控件模板,并试图管理焦点。这是一个糟糕的方法。
而不是你需要提取文本框控件模板和修改它通过添加水印元素以及一些触发器。
这里有一个非常粗略的例子。为了清晰起见,这个XAML被截断了,但应该给您一个概念:
如果你看到...
,那就是其他XAML布局和/或触发器的位置。
<ControlTemplate x:Key="TextBoxTemplate" TargetType="{x:Type TextBox}">
<Grid x:Name="RootElement"
SnapsToDevicePixels="True"
UseLayoutRounding="True">
...
<ScrollViewer x:Name="PART_ContentHost"
Padding="{TemplateBinding Padding}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
Background="Transparent"
BorderThickness="0"
Foreground="{TemplateBinding Foreground}" />
<TextBlock x:Name="WatermarkElement"
Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Foreground="{TemplateBinding Foreground}"
Opacity="0.5"
Text="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}"
Visibility="Collapsed" />
</Grid>
<ControlTemplate.Triggers>
...
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=Text, RelativeSource={RelativeSource Self}, Converter={StaticResource StringLengthToBoolConverter}}" Value="false" />
<Condition Binding="{Binding Path=Tag, RelativeSource={RelativeSource Self}, Converter={StaticResource StringLengthToBoolConverter}}" Value="true" />
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsKeyboardFocusWithin}" Value="false" />
</MultiDataTrigger.Conditions>
<Setter TargetName="WatermarkElement" Property="Visibility" Value="Visible" />
</MultiDataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
我的例子使用了一个名为StringLengthToBoolConverter
的值转换器,它计算IsNullOrEmpty
的字符串并返回true
或false
。