Привязка свойств в xaml
Я хочу сделать TextBox с водяный знаком, но не знаю как сделать установку водяного знака через xaml, пытался использовать привязку, но так и не получилось
XAML:
<UserControl x:Class="TestMVVM.WaterTextBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:TestMVVM"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<TextBox x:Name="noWater" VerticalAlignment="Top" HorizontalAlignment="Left" Width="100 "></TextBox>
<TextBlock x:Name="Water" IsHitTestVisible="False" Foreground="Gray" Margin="3 1 0 0">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName= noWater, Path= Text}" Value="">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
</UserControl>
C# :
public partial class WaterTextBox : UserControl
{
public string WaterMark { get; set; } = "Some Text"; // показывает, но изменять нельзя
public WaterTextBox()
{
InitializeComponent();
Water.Text = WaterMark;
}
}
MainWindow :
<Grid>
<!-- Не изменяется-->
<local:WaterTextBox WaterMark="Another Text"/>
</Grid>