C# 要将水印文本作为属性添加到WPF用户控件中吗?
我已经创建了一个WPF用户控件。该控件包含一个文本框和按钮。我想要那个控件中的water mark属性 这将帮助用户添加他们喜欢的文本作为水印。如水印=“输入密码…”C# 要将水印文本作为属性添加到WPF用户控件中吗?,c#,wpf,user-controls,watermark,C#,Wpf,User Controls,Watermark,我已经创建了一个WPF用户控件。该控件包含一个文本框和按钮。我想要那个控件中的water mark属性 这将帮助用户添加他们喜欢的文本作为水印。如水印=“输入密码…” 如何在用户控件中添加水印作为属性 .看看这个水印 基本上,在文本框上添加一个文本块,然后在不想再显示水印时将其隐藏 如果需要自定义文本,请创建依赖项属性并将其绑定到textblock的text属性。这样,用户可以指定他们想要的任何文本 public string WaterMark { get { return (str
如何在用户控件中添加水印作为属性
.看看这个水印 基本上,在文本框上添加一个文本块,然后在不想再显示水印时将其隐藏 如果需要自定义文本,请创建依赖项属性并将其绑定到textblock的
text
属性。这样,用户可以指定他们想要的任何文本
public string WaterMark
{
get { return (string )this.GetValue(WaterMarkProperty); }
set { this.SetValue(WaterMarkProperty, value); }
}
public static readonly DependencyProperty WaterMarkProperty = DependencyProperty.Register(
"WaterMark", typeof(string ), typeof(PasswordBoxWin8));
然后在XAML中绑定到它
<TextBlock Text="{Binding WaterMark, ElementName=YourUserControlName}" />
这样,您的用户控件有一个名为
WaterMark
的属性,您可以设置该属性尝试为控件添加样式:
资源:
<SolidColorBrush x:Key="watermarkBackground" Color="White" />
<SolidColorBrush x:Key="watermarkForeground" Color="LightSteelBlue" />
<SolidColorBrush x:Key="watermarkBorder" Color="Indigo" />
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
<Style x:Key="MyStyle" TargetType="Grid" >
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Margin" Value="20,0" />
</Style>
以及此样式在MainWindow.xaml中的用法
<Grid Background="{StaticResource watermarkBackground}" Style="{StaticResource MyStyle}" >
<TextBlock Text="Your water mark"
Foreground="{StaticResource watermarkForeground}"
Visibility="{Binding ElementName=txtUserEntry, Path=Text.IsEmpty, Converter={StaticResource BooleanToVisibilityConverter}}" />
<TextBox Background="Transparent" BorderBrush="{StaticResource watermarkBorder}" />
</Grid>
我已经看到了答案,但他们在用户控件本身中应用Text=“键入时出现此提示…”。但是我想把这个水印作为属性字段,比如名为水印的/Height。然后用户可以键入他们喜欢的内容..你可以下载我的Usercontrol Darn David,我正在搜索相同的答案。。。我确信我也看到了另一个非常类似的解决方案……:)你能用一个例子来解释这个问题吗?谢谢@aleksey,但请把问题再读一遍。我不是只要求水印。
<Grid Background="{StaticResource watermarkBackground}" Style="{StaticResource MyStyle}" >
<TextBlock Text="Your water mark"
Foreground="{StaticResource watermarkForeground}"
Visibility="{Binding ElementName=txtUserEntry, Path=Text.IsEmpty, Converter={StaticResource BooleanToVisibilityConverter}}" />
<TextBox Background="Transparent" BorderBrush="{StaticResource watermarkBorder}" />
</Grid>