我有CustomTextbox
的样式,我想应用我发现的解决方案添加“提示文字”又名“占位符”here。
我我非常接近最终解决方案,但似乎我在合并时缺少一些东西,也许对WPF有更好了解的人可以看到它。
<Style x:Key="placeHolder" TargetType="{x:Type Utils:CustomTextBox}">
<Setter Property="FontFamily"
Value="/UserInterface;component/Resources/Fonts/#Avenir LT Std 35 Light" />
<Setter Property="FontSize"
Value="16" />
<Setter Property="Foreground"
Value="#FF414042" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Utils:CustomTextBox}">
<Border Name="Border"
BorderBrush="#FF348781"
BorderThickness="0,0,0,4"
CornerRadius="2">
<ScrollViewer x:Name="PART_ContentHost"
Margin="0" />
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="Disabled" />
<VisualState x:Name="ReadOnly" />
<VisualState x:Name="MouseOver" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type Utils:CustomTextBox}" BasedOn="{StaticResource placeHolder}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid>
<Utils:CustomTextBox Text="{Binding Path=Text,
RelativeSource={RelativeSource TemplatedParent},
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
x:Name="textSource"
Background="Transparent"
Panel.ZIndex="2"
Style="{StaticResource placeHolder}"/>
<Utils:CustomTextBox Text="{TemplateBinding Tag}" Background="{TemplateBinding Background}" Panel.ZIndex="1" >
<Utils:CustomTextBox.Style>
<Style TargetType="{x:Type Utils:CustomTextBox}">
<Setter Property="Foreground" Value="Transparent"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Text, Source={x:Reference textSource}}" Value="">
<Setter Property="Foreground" Value="Gray"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Utils:CustomTextBox.Style>
</Utils:CustomTextBox>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
任何形式的帮助都会很有意义,如果有更好的方法可以很好地听到它,因为我愿意学习。
提前致谢。
答案 0 :(得分:0)
找到它,我在内部样式上缺少另一个BaseOn属性。
<Style TargetType="{x:Type Utils:CustomTextBox}" BasedOn="{StaticResource placeHolder}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid>
<Utils:CustomTextBox Text="{Binding Path=Text,
RelativeSource={RelativeSource TemplatedParent},
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
x:Name="textSource"
Background="Transparent"
Panel.ZIndex="2"
Style="{StaticResource placeHolder}"/>
<Utils:CustomTextBox Text="{TemplateBinding Tag}" Background="{TemplateBinding Background}" Panel.ZIndex="1" >
<Utils:CustomTextBox.Style>
<Style TargetType="{x:Type Utils:CustomTextBox}" BasedOn="{StaticResource placeHolder}">
<Setter Property="Foreground" Value="Transparent"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Text, Source={x:Reference textSource}}" Value="">
<Setter Property="Foreground" Value="Gray"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Utils:CustomTextBox.Style>
</Utils:CustomTextBox>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>