gpt4 book ai didi

wpf - PasswordBox 不假设样式

转载 作者:行者123 更新时间:2023-12-04 04:30:58 27 4
gpt4 key购买 nike

我有以下样式定义:

<!-- Border -->
<Style x:Key="MyControlBorder" TargetType="{x:Type Border}">
<Setter Property="BorderBrush" Value="DarkKhaki" />
<Setter Property="Background" Value="White" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="CornerRadius" Value="10" />
</Style>

<!-- TextBox -->
<Style x:Key="MyTextBox" TargetType="{x:Type TextBox}">
<Setter Property="Height" Value="30" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBoxBase}">
<Border Name="TextBoxBorder" Style="{StaticResource MyControlBorder}">
<ScrollViewer x:Name="PART_ContentHost"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

<!-- PasswordBox -->
<Style x:Key="MyPasswordBox" TargetType="{x:Type PasswordBox}">
<Setter Property="Height" Value="30" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Control}">
<Border Name="Border" Style="{StaticResource MyControlBorder}">
<ScrollViewer x:Name="PART_ContentHost" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

以及以下 xaml 代码:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Style="{StaticResource MyTextBox}" />
<PasswordBox Grid.Row="1" Style="{StaticResource MyPasswordBox}" />
</Grid>

现在我得到了这个结果:
result

TextBox 正确地采用了样式,但为什么 PasswordBox 不采用该样式?

最佳答案

不知何故 BorderControlTemplatePasswordBox不带MyControlBorder风格。

修改时MyPasswordBox这样的风格......然后它会起作用。

<Style x:Key="MyPasswordBox" TargetType="{x:Type PasswordBox}">
<Setter Property="Height" Value="30" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Control}">
<Border Name="Border" BorderBrush="DarkKhaki" Background="White" BorderThickness="1" CornerRadius="10">
<ScrollViewer x:Name="PART_ContentHost" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>

我知道这不是最好的解决方案...但我不明白为什么 MyControlBorder不适用。当你摆脱 MyTextBox 时它甚至不起作用风格。那么你只剩下 MyControlBorderMyPasswordBox ......它也不起作用。

关于wpf - PasswordBox 不假设样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16911345/

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