PasswordBox不采用样式 [英] PasswordBox does not assume style

查看:77
本文介绍了PasswordBox不采用样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下样式定义:

<!-- 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代码:

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:

TextBox正确采用样式,但是为什么PasswordBox不采用样式?

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

推荐答案

以某种方式PasswordBoxControlTemplate中的Border不采用MyControlBorder样式.

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

当您像这样修改MyPasswordBox样式时...它将起作用.

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>

我知道这不是最好的解决方案...但是我不知道为什么不应用MyControlBorder的原因.当您摆脱MyTextBox样式时,它甚至不起作用.然后只剩下MyControlBorderMyPasswordBox ...它也不起作用.

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.

这篇关于PasswordBox不采用样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆