,我認爲這會工作,我想你想實現你的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);
}
}
變化觸發了PasswordBox
的Style
以下幾點:
<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>
這非常的感謝!之前沒有這樣做,所以不太確定該怎麼做! –
嗯,不工作在我的密碼箱,我已經明顯改變它,就像這樣:''但它不工作 –
啊,密碼被密封並且觸發器不起作用:( –