【问题标题】:DataTrigger on TextBlock not working as expectedTextBlock 上的 DataTrigger 未按预期工作
【发布时间】:2017-02-13 13:32:03
【问题描述】:

我正在尝试通过以下代码为 PasswordBox 实现占位符文本:

<PasswordBox x:Name="passwordText"/>
<TextBlock IsHitTestVisible="False" Text="Password">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Visibility" Value="Collapsed"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Password, ElementName=passwordText}" Value="">
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

结果如下:

那么,上面的代码有问题吗?

顺便说一句,我用 TextBox 而不是 PasswordBox 进行了尝试,它按预期工作。

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    正如@Anjum 所提到的。出于某些安全原因,PasswordBox 中的 Password 属性不是依赖属性。所以它不会通知Trigger 的任何变化。

    一种解决方法是

    <Window.Resources>
        <Style TargetType="{x:Type PasswordBox}" xmlns:sys="clr-namespace:System;assembly=mscorlib">
            <Style.Resources>
                <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
                    <VisualBrush.Visual>
                        <Label Content="Password" Foreground="LightGray"/>
                    </VisualBrush.Visual>
                </VisualBrush>
            </Style.Resources>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Password}" Value="{x:Null}">
                    <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                </DataTrigger>
    
                <Trigger Property="IsKeyboardFocused" Value="True">
                    <Setter Property="Background" Value="White" />
                </Trigger>
            </Style.Triggers>
            <Setter Property="Control.Foreground" Value="#4C2C66"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>
    </Window.Resources>
    
    <PasswordBox Width="200" Height="75" />
    

    来源:

    https://stackoverflow.com/a/20342141/2819451

    https://stackoverflow.com/a/27167280/2819451

    【讨论】:

    • 谢谢,它“有点”工作。但这不是一行:Binding="{Binding Path=Password}" using Password 在 Trigger 中的属性吗?
    • @ZolbayarBayarsaikhan 对不起。实际上,它不会通知更改。
    【解决方案2】:

    编辑 #1

    在用户clemens发表评论后,我更正了我的回答语言。

    Password 属性没有更改通知,所以在这里不起作用。 Password 属性不是 DP。

    您必须使用事件处理程序的普通方法。使用PasswordBox.PasswordChangedevent。

    【讨论】:

    • 密码属性没有的绑定。密码是绑定的源属性,因此不需要是依赖属性。但是,由于它不是 DP,因此也没有会触发 DataTrigger 的绑定的更改通知。
    • @Clemens 我没有注意到我的语言,现在更正了。
    • 在调整了@Gopichandar 发布的代码之后,现在我决定使用PasswordChanged 事件。感谢您的建议
    【解决方案3】:

    正如@AnjumSKhan 建议的那样,我已经通过PasswordBox.PasswordChanged 事件完成了它。如果有人遇到类似问题,这里是为PasswordBox 添加占位符的代码。

    在 xaml 文件中:

    <Window.Resources>
        <VisualBrush x:Key="PasswordPlaceHolderBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
            <VisualBrush.Visual>
                <Label Content="Нууц үг" Foreground="DarkGray"/>
            </VisualBrush.Visual>
        </VisualBrush>
    </Window.Resources>
    
    <PasswordBox PasswordChanged="passwordText_PasswordChanged" 
        Background="{StaticResource PasswordPlaceHolderBrush}"  x:Name="passwordText"/>
    

    在后面的代码中:

    private void passwordText_PasswordChanged(object sender, RoutedEventArgs e)
    {
        PasswordBox senderOb = (PasswordBox)sender; 
        if(senderOb.Password == "")
        {
            passwordText.Background = (VisualBrush) FindResource("PasswordPlaceHolderBrush");
        }
        else
        {
            passwordText.Background = Brushes.White; 
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-27
      • 2010-09-10
      • 2018-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      • 2021-06-04
      相关资源
      最近更新 更多