2015-11-26 29 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

啊,密碼被密封並且觸發器不起作用:( –