2015-11-26 28 views
0

如何在使用XAML触发器来隐藏文本块时,将文本输入到我的样式文本框中。这是我现在得到的代码,它不工作,没有错误,可能是错误的。我如何去做这件事?WPF隐藏文本块如果文本输入到TextBox触发器

<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> 

回答

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“ s Text.Count为0(表示未输入任何值),则显示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); 
    } 
} 

变化触发了PasswordBoxStyle以下几点:

<DataTrigger Binding="{Binding (local:PasswordBoxAttachedProperties.IsPasswordEntered), RelativeSource={RelativeSource Self}}" Value="False"> 
    <Setter TargetName="PART_TempText" Property="Visibility" Value="Visible" /> 
</DataTrigger> 

local是您在您的应用中使用的命名空间映射lication。

添加到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> 
+0

这非常的感谢!之前没有这样做,所以不太确定该怎么做! –

+0

嗯,不工作在我的密码箱,我已经明显改变它,就像这样:''但它不工作 –

+0

啊,密码被密封并且触发器不起作用:( –