在WPF应用程序中,我试图在我设置了自定义样式的MahApps.Metro文本框中添加水印:
Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="VerticalAlignment" Value="Center" />
<!--<Setter Property="BorderThickness" Value="1" />-->
<Setter Property="Margin" Value="10 0 30 0" />
<!--<Setter Property="BorderBrush" Value="SteelBlue" />-->
<Setter Property="BorderThickness" Value="0.7" />
<Setter Property="BorderBrush" Value="Gray" />
<Setter Property="Background" Value="AliceBlue" />
<Setter Property="Padding" Value="5" />
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="true">
<Border Background="OrangeRed" DockPanel.Dock="right" Margin="5,0,0,0"
Width="20" Height="20" CornerRadius="5"
ToolTip="{Binding ElementName=customAdorner,
Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
<TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center"
FontWeight="Bold" Foreground="white" />
</Border>
<AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" >
<Border BorderBrush="red" BorderThickness="1" />
</AdornedElementPlaceholder>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Opacity" Value="0.56"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="BorderBrush" Value="{DynamicResource AccentColorBrush}"/>
</Trigger>
<Trigger Property="IsFocused" Value="true">
<Setter Property="BorderBrush" Value="{DynamicResource AccentColorBrush}"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="MenuLevel2" BasedOn="{StaticResource MetroTabItem}" TargetType="{x:Type TabItem}">
<Setter Property="mah:ControlsHelper.HeaderFontSize" Value="16" />
<Setter Property="mah:ControlsHelper.HeaderFontWeight" Value="Normal"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Foreground" Value="SteelBlue"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
</Trigger>
</Style.Triggers>
</Style>
<TextBox Grid.Column="1" Grid.Row="1" x:Name="txtBox_updateWebSrvURL" Style="{StaticResource TextBoxStyle}"
Validation.Error="Validation_Error"
Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=webSrvURL,
ValidatesOnDataErrors=true, NotifyOnValidationError=true}"
Controls:TextBoxHelper.Watermark="MyWatermark here!!!!/" />
如果删除自定义样式,则水印会很好地应用。知道如何启用水印但仍使用我的自定义样式吗?
答案 0 :(得分:1)
<Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="VerticalAlignment" Value="Center" />
<!--<Setter Property="BorderThickness" Value="1" />-->
<Setter Property="Margin" Value="10 0 30 0" />
<!--<Setter Property="BorderBrush" Value="SteelBlue" />-->
<Setter Property="BorderThickness" Value="0.7" />
<Setter Property="BorderBrush" Value="Gray" />
<Setter Property="Background" Value="AliceBlue" />
<Setter Property="Padding" Value="5" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid>
<TextBox Text="{Binding Path=Text,
RelativeSource={RelativeSource TemplatedParent},
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
x:Name="textSource"
Background="Transparent"
Panel.ZIndex="2" />
<TextBox Text="{TemplateBinding Tag}" x:Name="placeholdertext" VerticalContentAlignment="Center" Background="{TemplateBinding Background}" Panel.ZIndex="1">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="Transparent"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Text, Source={x:Reference textSource}}" Value="">
<Setter Property="Foreground" Value="LightGray"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Text, Source={x:Reference textSource}}" Value="!">
<Setter Property="Opacity" Value="0"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="true">
<Border Background="OrangeRed" DockPanel.Dock="right" Margin="5,0,0,0"
Width="20" Height="20" CornerRadius="5"
ToolTip="{Binding ElementName=customAdorner,
Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
<TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center"
FontWeight="Bold" Foreground="white" />
</Border>
<AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" >
<Border BorderBrush="red" BorderThickness="1" />
</AdornedElementPlaceholder>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Opacity" Value="0.56"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="BorderBrush" Value="{DynamicResource AccentColorBrush}"/>
</Trigger>
<Trigger Property="IsFocused" Value="true">
<Setter Property="BorderBrush" Value="{DynamicResource AccentColorBrush}"/>
</Trigger>
</Style.Triggers>
</Style>
文本框
<TextBox Grid.Column="1" Grid.Row="1" x:Name="txtBox_updateWebSrvURL" Style="{StaticResource TextBoxStyle}"
Validation.Error="Validation_Error"
Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=webSrvURL,
ValidatesOnDataErrors=true, NotifyOnValidationError=true}"
Tag="MyWatermark here!!!!/" />