【问题标题】:Styling a WPF Passwordbox样式化 WPF 密码框
【发布时间】:2015-07-26 02:49:28
【问题描述】:

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

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

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

这是我的样式代码:

<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>

【问题讨论】:

标签: c# wpf xaml passwordbox


【解决方案1】:

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

来自PasswordBox Syles and Templates

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

然后将Foreground 更改为您Style 中的另一个Setter。另外,作为一个侧节点,我会对Background 做同样的事情,并在你的ControlTemplate 中使用TemplateBinding。这将提供更大的灵活性,并允许您手动更改 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>

【讨论】:

  • 像魅力一样工作!谢谢! :)
猜你喜欢
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
  • 1970-01-01
  • 1970-01-01
  • 2012-04-17
  • 2011-01-28
相关资源
最近更新 更多