PasswordBox不承担风格(PasswordBox does not assume style)

编程入门 行业动态 更新时间:2024-10-10 14:30:42
PasswordBox不承担风格(PasswordBox does not assume style)

我有以下样式定义:

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

现在我得到了这个结果:

TextBox正确地呈现样式,但为什么PasswordBox不承担样式?

I have the following style definitions:

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

and the following xaml code:

<Grid> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <TextBox Grid.Row="0" Style="{StaticResource MyTextBox}" /> <PasswordBox Grid.Row="1" Style="{StaticResource MyPasswordBox}" /> </Grid>

Now i got this results:

The TextBox assume the style correctly, but why the PasswordBox does not assume the style?

最满意答案

不知怎么,在PasswordBox ControlTemplate的Border不会采用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样式时它甚至不起作用。 然后你只剩下MyControlBorder和MyPasswordBox ...它也不起作用。

Somehow Border within ControlTemplate of PasswordBox does not take MyControlBorder style.

When you modify MyPasswordBox style like this... then it will work.

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

I know it's not the best solution... but I can't figure out why MyControlBorder is not applied. It doesn't even work when you get rid of MyTextBox style. Then you are left only with MyControlBorder and MyPasswordBox ...it does not work either.

更多推荐

TextBox,PasswordBox,电脑培训,计算机培训,IT培训"/> <meta name="descripti

本文发布于:2023-08-07 11:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464169.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不承担   风格   PasswordBox   assume   style

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!