WPF中如何对Passwordbox进行样式设置

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>

这个有帮助吗?https://dev59.com/MWs05IYBdhLWcg3wIehj - Sriram Sakthivel
1个回答

3
如果您为PasswordBoxTextBox创建自定义ControlTemplate,则需要将名为x:Name="PART_ContentHost"的ScrollViewer放置在内部PasswordBox而不是内部。从{{link1:PasswordBox Syles and Templates}}中了解更多信息。

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

同时,将Foreground更改为Style中的另一个Setter。此外,作为副作用,请在您的ControlTemplate中使用TemplateBinding更改Background。这将提供更大的灵活性,并允许您手动更改Background和/或Foreground而不更改ControlTemplate
<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>

非常好用!谢谢! :) - RunicSheep

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接