如果将文本输入到TextBox触发器中,则WPF隐藏TextBlock

时间:2015-11-26 22:23:19

标签: wpf xaml

如果在我的Style中将文本输入TextBox时,如何使用XAML触发器隐藏TextBlock。这是我现在的代码,它不起作用,没有错误,可能只是错误。我该怎么做呢?

<Style TargetType="TextBox">
        <Setter Property="Background" Value="#FF22252C" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="Width" Value="200" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TextBox">
                    <Border CornerRadius="5" Background="#FF22252C" Margin="3" MinWidth="{TemplateBinding MinWidth}">
                        <Grid>
                            <StackPanel Margin="8">
                                <ScrollViewer x:Name="PART_ContentHost"/>
                            </StackPanel>
                            <StackPanel>
                                <TextBlock Name="PART_TempText" Text="{TemplateBinding Name}" Foreground="#FF454954" Padding="8" />
                            </StackPanel>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="Text" Value="{x:Null}">
                            <Setter TargetName="PART_TempText" Property="Foreground" Value="Red" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

1 个答案:

答案 0 :(得分:2)

我认为这样可行,我假设您希望在TextBox

中实现占位符/水印功能
<Style x:Key="CustomTextBoxStyle" TargetType="TextBox">
    <Setter Property="Background" Value="#FF22252C" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Width" Value="200" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TextBox">
                <Border CornerRadius="5" Background="#FF22252C" Margin="3" MinWidth="{TemplateBinding MinWidth}">
                    <Grid>
                        <StackPanel Margin="8">
                            <ScrollViewer x:Name="PART_ContentHost"/>
                        </StackPanel>
                        <StackPanel>
                            <TextBlock Name="PART_TempText" Text="{TemplateBinding Name}" Foreground="#FF454954"
                                       Visibility="Collapsed"
                                       Padding="8" />
                        </StackPanel>
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <DataTrigger Binding="{Binding Text.Count, RelativeSource={RelativeSource Self}}" Value="0">
                        <Setter TargetName="PART_TempText" Property="Visibility" Value="Visible" />
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我们的想法是,您最初隐藏TextBlock,如果TextBox的{​​{1}}为0(表示未输入任何值),则会显示Text.Count

更新

我对你提到的关于TextBlock的问题有一个解决方案,也许它不是最漂亮的(不是),但我会分享它:)

它不起作用的原因,是因为

一种可能的解决方案如下:

为项目添加新的附加属性:

PasswordBox

public class PasswordBoxAttachedProperties { public static readonly DependencyProperty IsPasswordEnteredProperty = DependencyProperty.RegisterAttached( "IsPasswordEntered", typeof (bool), typeof (PasswordBoxAttachedProperties), new PropertyMetadata(default(bool))); public static void SetIsPasswordEntered(DependencyObject element, bool value) { element.SetValue(IsPasswordEnteredProperty, value); } public static bool GetIsPasswordEntered(DependencyObject element) { return (bool) element.GetValue(IsPasswordEnteredProperty); } } 的{​​{1}}中的触发器更改为以下内容:

PasswordBox

Style是您在应用程序中使用的命名空间映射。

添加对System.Windows.Interactivity的引用并创建以下<DataTrigger Binding="{Binding (local:PasswordBoxAttachedProperties.IsPasswordEntered), RelativeSource={RelativeSource Self}}" Value="False"> <Setter TargetName="PART_TempText" Property="Visibility" Value="Visible" /> </DataTrigger>

local

最后,在TriggerAction

中添加此触发器
public class NotifyPasswordChangeTrigger : TriggerAction<PasswordBox>
{
    protected override void Invoke(object parameter)
    {
        AssociatedObject.SetValue(PasswordBoxAttachedProperties.IsPasswordEnteredProperty, !string.IsNullOrEmpty(AssociatedObject.Password));
    }
}

PS:我认为你不应该使用PasswordBox属性作为占位符/水印。可能你应该为它创建一个新的附加属性,这样你就可以像这样使用它(并将<PasswordBox Name="Password"> <i:Interaction.Triggers> <i:EventTrigger EventName="PasswordChanged"> <local:NotifyPasswordChangeTrigger /> </i:EventTrigger> </i:Interaction.Triggers> </PasswordBox> 中的绑定替换为你的样式中的新附加属性):

Name