代码之家  ›  专栏  ›  技术社区  ›  Salim

具有定义样式的wpf水印mahaps

  •  1
  • Salim  · 技术社区  · 6 年前

    在我的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!!!!/" />
    

    如果删除自定义样式,则水印应用得很好。知道如何启用水印但仍使用自定义样式吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Shubham Sahu    6 年前
    <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!!!!/" />