4

我目前正在尝试执行以下操作:如果未输入密码,则应显示文本“密码”。

但是我的模板没有显示密码,如果我使用装饰器或滚动查看器,我无法更改文本的颜色。

你有什么想法来实现这一目标吗?

这是我的样式代码:

<Style TargetType="{x:Type PasswordBox}" x:Key="Password">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="PasswordBox">
                    <Grid>
                        <PasswordBox Background="{StaticResource BrushDark}" Foreground="{StaticResource BrushTextNormal}" BorderBrush="{StaticResource BrushBorderInput}" BorderThickness="1"/>
                        <TextBlock HorizontalAlignment="Left"
                            VerticalAlignment="Center"
                            Text="Password"
                            Margin="5,0,5,0"
                            Foreground="#ff808080"
                            IsHitTestVisible="False"
                            x:Name="UserMessage"
                            Visibility="Hidden"/>
                        <!--<ScrollViewer Foreground="{StaticResource BrushTextNormal}" Background="{StaticResource BrushTextNormal}" x:Name="PART_ContentHost"/>-->
                        <!--<Decorator TextBlock.Foreground="White" x:Name="PART_ContentHost"/>-->
                    </Grid>
                    <ControlTemplate.Triggers>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="Tag" Value=""/>
                                <Condition Property="IsKeyboardFocusWithin" Value="False"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Visibility" TargetName="UserMessage" Value="Visible"/>
                        </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
4

1 回答 1

3

如果您创建自定义ControlTemplate或者PasswordBoxTextBox需要将ScrollViewer命名x:Name="PART_ContentHost而不是内部PasswordBox

来自PasswordBox 样式和模板

PART_ContentHost - 可以包含 FrameworkElement 的可视元素。PasswordBox 的文本显示在此元素中。

并在你身上改变Foreground为另一个人。另外,作为一个侧节点,我会在你的. 这将提供更大的灵活性,并允许您在不更改的情况下进行更改和/或手动更改SetterStyleBackgroundTemplateBindingControlTemplateBackgroundForegroundControlTemplate

<Style TargetType="{x:Type PasswordBox}" x:Key="Password">
   <Setter Property="Foreground" Value="{StaticResource BrushTextNormal}" />
   <Setter Property="Background" Value="{StaticResource BrushDark}"/>
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type PasswordBox}">
            <Grid Background="{TemplateBinding Background}">
               <ScrollViewer x:Name="PART_ContentHost" .../>
               <TextBlock .../>               
            </Grid>
            <ControlTemplate.Triggers>
               <!-- removed -->
            </ControlTemplate.Triggers>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style>
于 2015-05-14T13:11:01.477 回答