简体   繁体   English

覆盖 XAML 中自定义控件样式的属性

[英]Override property of custom control style in XAML

I borrowed some code from some other guy here on Stack Overflow.我从 Stack Overflow 上的其他人那里借用了一些代码。 I have two PasswordBoxes.我有两个密码框。 I want the first one to show "Password" and the second one to show "Re-enter Password".我希望第一个显示“密码”,第二个显示“重新输入密码”。 I don't want to rewrite the complete style all over again if the only difference is the text in the TextBlock.如果唯一的区别是 TextBlock 中的文本,我不想重新编写完整的样式。 How can I override and change the value of the TextBlock, if the TargetType has to be PasswordBox?如果 TargetType 必须是 PasswordBox,如何覆盖和更改 TextBlock 的值? I'm trying to create a second style that is based on the first one, and then change it from there, but I'm not sure about the syntax.我正在尝试创建基于第一个样式的第二个样式,然后从那里更改它,但我不确定语法。

This one works fine:这个工作正常:

    <Style x:Name="customPWBStyle" x:Key="customPasswordBox" 
    TargetType="{x:Type PasswordBox}">
        <Setter Property="helper:PasswordBoxMonitor.IsMonitoring"
          Value="True"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type PasswordBox}">
                    <Border Name="Bd"
                Background="{TemplateBinding Background}"
                BorderThickness="{TemplateBinding BorderThickness}"
                BorderBrush="{TemplateBinding BorderBrush}"
                SnapsToDevicePixels="true">
                        <Grid>
                            <ScrollViewer x:Name="PART_ContentHost"
                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            <TextBlock Text="Password" 
                       Margin="4, 2, 0, 0"
                       Foreground="Gray" 
                       Visibility="Collapsed"
                       Name="txtPrompt" />
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled"
                                     Value="false">
                            <Setter TargetName="Bd"
                                        Property="Background"
                                        Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                            <Setter Property="Foreground"
                                        Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                        <Trigger Property="helper:PasswordBoxMonitor.PasswordLength" Value="0">
                            <Setter Property="Visibility" TargetName="txtPrompt" Value="Visible"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

But I want to create another style identical, but the only difference has to be the TextBlock that has to say "Re-enter password"但我想创建另一种相同的样式,但唯一的区别必须是必须说“重新输入密码”的 TextBlock

This is what I got so far:这是我到目前为止得到的:

        <Style x:Key="reEnterPasswordBox" BasedOn="{StaticResource customPasswordBox}" TargetType="{x:Type PasswordBox}">
        <Style.Resources>
            <Style TargetType="TextBlock">
                <Setter Property="Text" Value="Re-enter Password"></Setter>
            </Style>
        </Style.Resources>            
    </Style>

However it does not work.但是它不起作用。 I can see that theres a name for the TextBlock, which is txtPrompt, but I'm not sure if its possible to use that as a reference for chaning the value of the TextBlock.我可以看到 TextBlock 有一个名称,即 txtPrompt,但我不确定是否可以将其用作更改 TextBlock 值的参考。

I would recommend to create a special dependency property in customPasswordBox, eg InputHint.我建议在 customPasswordBox 中创建一个特殊的依赖属性,例如 InputHint。 (If you can't change customPasswordBox code, make a custom attached dependency property - like helper:PasswordBoxMonitor.IsMonitoring. Attached DPs are great for parametrizing templates) (如果您无法更改 customPasswordBox 代码,请创建自定义附加依赖项属性 - 如 helper:PasswordBoxMonitor.IsMonitoring。附加 DP 非常适合参数化模板)

When you have a property, set default value via Setter, and then bind TextBlock to it via TemplateBinding.当你有一个属性时,通过 Setter 设置默认值,然后通过 TemplateBinding 将 TextBlock 绑定到它。

<Style x:Name="customPWBStyle" x:Key="customPasswordBox" 
       TargetType="{x:Type PasswordBox}">
    <Setter Property="helper:PasswordBoxMonitor.IsMonitoring" Value="True"/>
    <Setter Property="InputHint" Value="Password"/>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type PasswordBox}">
                <Border Name="Bd"
                        Background="{TemplateBinding Background}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        SnapsToDevicePixels="true">
                    <Grid>
                        <ScrollViewer x:Name="PART_ContentHost"
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        <TextBlock Text="{TemplateBinding InputHint}" 
                                   Margin="4, 2, 0, 0"
                                   Foreground="Gray" 
                                   Visibility="Collapsed"
                                   Name="txtPrompt" />
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled"
                                 Value="false">
                        <Setter TargetName="Bd"
                                    Property="Background"
                                    Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                        <Setter Property="Foreground"
                                    Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                    <Trigger Property="helper:PasswordBoxMonitor.PasswordLength" Value="0">
                        <Setter Property="Visibility" TargetName="txtPrompt" Value="Visible"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

to make another style, change only Setter for InputHint:要制作另一种样式,只需更改 InputHint 的 Setter:

<Style x:Key="reEnterPasswordBox" BasedOn="{StaticResource customPasswordBox}" TargetType="{x:Type PasswordBox}">
    <Setter Property="InputHint" Value="Re-enter Password"/>          
</Style>

Parts of template are not easily reachable for modification, even with implicit styles即使使用隐式样式,模板的某些部分也不容易修改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM