[英]WPF Hide TextBlock if Text is entered into TextBox Trigger
当以“样式”将文本输入到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>
我认为这可行,我想您想在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
的Text.Count
为0(表示未输入任何值),则显示TextBlock
。
对于您提到的有关PasswordBox
的问题,我有解决方案,也许它不是最漂亮的(不是),但我还是会分享它的:)
之所以不起作用,是因为
Password.Length
PasswordBox
不公开DependencyProperties,因为它会造成安全风险( https://stackoverflow.com/a/1493330/4620101 ) 一种可能的解决方案如下:
将新的附加属性添加到您的项目中:
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);
}
}
将PasswordBox
的Style
的触发器更改为以下内容:
<DataTrigger Binding="{Binding (local:PasswordBoxAttachedProperties.IsPasswordEntered), RelativeSource={RelativeSource Self}}" Value="False">
<Setter TargetName="PART_TempText" Property="Visibility" Value="Visible" />
</DataTrigger>
local
是您在应用程序中使用的名称空间映射。
添加对System.Windows.Interactivity的引用,并创建以下TriggerAction
:
public class NotifyPasswordChangeTrigger : TriggerAction<PasswordBox>
{
protected override void Invoke(object parameter)
{
AssociatedObject.SetValue(PasswordBoxAttachedProperties.IsPasswordEnteredProperty, !string.IsNullOrEmpty(AssociatedObject.Password));
}
}
最后,在您的PasswordBox
添加此触发器:
<PasswordBox Name="Password">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PasswordChanged">
<local:NotifyPasswordChangeTrigger />
</i:EventTrigger>
</i:Interaction.Triggers>
</PasswordBox>
PS .:我认为您不应该将Name
属性用作占位符/水印。 可能您应该为此创建一个新的附加属性,以便可以像这样使用它(并用您的样式替换从Name
到新附加属性的绑定):
<PasswordBox local:TextBoxAttachedProperties.Placeholder="Please enter password...">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PasswordChanged">
<local:NotifyPasswordChangeTrigger />
</i:EventTrigger>
</i:Interaction.Triggers>
</PasswordBox>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.