Hi,@Sarah . If you want to use code like below, you need to add DependencyProperty WatterMarkText to PasswordBox.
<PasswordBox WatterMarkText ="Hello World" />
A common approach is to add Attached Dependency Property to the project.
public class Helper
{
public static string GetWatterMarkText(DependencyObject obj)
{
return (string)obj.GetValue(WatterMarkTextProperty);
}
public static void SetWatterMarkText(DependencyObject obj, string value)
{
obj.SetValue(WatterMarkTextProperty, value);
}
public static readonly DependencyProperty WatterMarkTextProperty =
DependencyProperty.RegisterAttached("WatterMarkText", typeof(string), typeof(Helper),
new FrameworkPropertyMetadata(" ", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
}
Update your code as follows.
<TextBox Text="{Binding Path=(local:Helper.WatterMarkText), RelativeSource={RelativeSource TemplatedParent}}"
Style="{StaticResource PwBoxWatterMarkTextStyle}" Name="txtPrompt">
Styles added for testing.
Password :
<Grid>
<PasswordBox Width="100" Height="50" local:Helper.WatterMarkText="Hello World" Style="{StaticResource PasswordWatermark}"/>
</Grid>
The result:
By the way, you can pay attention to the difference between Template Binding and Binding.
----------------------------------------------------------------------------
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.