gpt4 book ai didi

c# - 样式化 WPF 密码框

转载 作者:行者123 更新时间:2023-11-30 16:07:52 27 4
gpt4 key购买 nike

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

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

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

这是我的样式代码:

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

最佳答案

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

来自 PasswordBox Syles and Templates

PART_ContentHost - A visual element that can contain a FrameworkElement. The text of the PasswordBox is displayed in this element.

并将 Foreground 更改为 Style 中的另一个 Setter。此外,作为辅助节点,我会对 Background 执行相同的操作,并在您的 ControlTemplate 中使用 TemplateBinding。这将提供更大的灵 active ,并允许您手动更改 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>

关于c# - 样式化 WPF 密码框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30238149/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com