【问题标题】:Style Trigger on PasswordboxPasswordbox 上的样式触发器
【发布时间】:2013-12-18 21:21:37
【问题描述】:

我在文本框上使用以下样式,以便它具有文本和背景颜色,直到有人尝试向其中输入数据。工作正常,但我的问题出现了,因为它是一个登录屏幕,而我的另一个控件是一个密码框,它不允许我访问密码属性(相当于文本框的文本属性)。关于如何解决这个问题的任何建议?

<Style TargetType="TextBox" xmlns:sys="clr-namespace:System;assembly=mscorlib">
            <Style.Resources>
                <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
                    <VisualBrush.Visual>
                        <Label Content="Search" Foreground="LightGray"/>
                    </VisualBrush.Visual>
                </VisualBrush>
            </Style.Resources>
            <Style.Triggers>
                <Trigger Property="Text" Value="{x:Static sys:String.Empty}">
                    <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                </Trigger>
                <Trigger Property="Text" Value="{x:Null}">
                    <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                </Trigger>
                <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>

【问题讨论】:

标签: wpf textbox


【解决方案1】:

如果您使用的是PasswordBox,请务必更改TargetType 并使用DataTrigger

<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="Search" 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>

【讨论】:

  • 如果文本框为空,这会改变颜色......但即使输入了某些内容,它也会保持该颜色。如果输入了一些东西,你想改变颜色怎么办?
  • 在这种情况下,您可能需要使用转换器。 stackoverflow.com/a/356690/3990948
猜你喜欢
  • 2017-10-02
  • 2012-04-23
  • 1970-01-01
  • 1970-01-01
  • 2010-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多