Вот так всё работает:
<UserControl
x:Name="textBoxWithPlaceholder"
x:Class="LibControls.TextBoxWithPlaceholder"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Padding="5,0"
mc:Ignorable="d"
d:Width="200"
d:Height="30"
Background="White" >
<!-- Мой вариант UserControl На основе контейнера Grid c использованием тригеров -->
<!--Общий контейнер визуально объединяющий TextBlock и TextBox-->
<Grid x:Name="PART_grid">
<!--TextBlock для текста по умолчанию
Foreground - Цвет шрифта для текста по умолчанию-->
<TextBlock
x:Name="PART_TextBlock"
Foreground="LightGray"
Text="{Binding DefaultText, ElementName=textBoxWithPlaceholder}"
FontStyle="{Binding FontStyle, ElementName=textBoxWithPlaceholder}"
FontSize="{Binding FontSize, ElementName=textBoxWithPlaceholder}"
FontFamily="{Binding FontFamily, ElementName=textBoxWithPlaceholder}"
FontStretch="{Binding FontStretch, ElementName=textBoxWithPlaceholder}"/>
<!--TextBox для ввода-->
<TextBox
x:Name="PART_TextBox"
Background="Transparent"
Foreground="{Binding Foreground, ElementName=textBoxWithPlaceholder}"
FontStyle="{Binding FontStyle, ElementName=textBoxWithPlaceholder}"
FontSize="{Binding FontSize, ElementName=textBoxWithPlaceholder}"
FontFamily="{Binding FontFamily, ElementName=textBoxWithPlaceholder}"
FontStretch="{Binding FontStretch, ElementName=textBoxWithPlaceholder}"
Text="{Binding Text, ElementName=textBoxWithPlaceholder, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
BorderThickness="0"/>
</Grid>
</UserControl>
using System;
using System.Windows;
using System.Windows.Controls;
namespace LibControls
{
/// <summary>
/// Interaction logic for TextBoxWithPlaceholder.xaml
/// </summary>
public partial class TextBoxWithPlaceholder : UserControl
{
public TextBoxWithPlaceholder()
{
InitializeComponent();
}
public string DefaultText
{
get => (string)GetValue(DefaultTextProperty);
set => SetValue(DefaultTextProperty, value);
}
// Using a DependencyProperty as the backing store for DefaultText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DefaultTextProperty =
DependencyProperty.Register("DefaultText",
typeof(string), typeof(TextBoxWithPlaceholder));
/// <summary>
/// Свойство для текста ввода
/// </summary>
public string Text
{
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text",
typeof(string), typeof(TextBoxWithPlaceholder),
new PropertyMetadata(Text_Changed));
/// <summary>
/// Обработчик события изменения текста, устанавливающий
/// видимость/невидимость текста подсказки
/// </summary>
private static void Text_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TextBoxWithPlaceholder control
&& e.NewValue is string text)
{
if (string.IsNullOrWhiteSpace(text))
{
control.PART_TextBlock.Visibility = Visibility.Visible;
}
else
{
control.PART_TextBlock.Visibility = Visibility.Hidden;
}
}
}
}
}
<Window
x:Name="mainWindow"
x:Class="LibControls.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:LibControls"
mc:Ignorable="d"
d:DataContext="{d:DesignData Type=local.LoginWindowContext, IsDesignTimeCreatable=False}"
Title="MainWindow" Height="450" Width="800">
<DockPanel
HorizontalAlignment="Center"
VerticalAlignment="Center">
<local:TextBoxWithPlaceholder
BorderBrush="Black"
BorderThickness="1"
Margin="30"
Padding="20 5"
DefaultText="Логин"
Text="{Binding Path=Login,Mode=TwoWay}"/>
<TextBox
BorderBrush="Black"
BorderThickness="1"
Margin="30"
Padding="20 5"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="{Binding Path=Login,Mode=TwoWay}"/>
</DockPanel>
</Window>
using System;
using System.Windows;
namespace LibControls
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new LoginWindowContext();
}
}
}
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace LibControls
{
internal class LoginWindowContext : INotifyPropertyChanged
{
private string _Login = "dfgjhbdsfgjhb";
public string Login
{
get => _Login;
set
{
Set(ref _Login, value);
}
}
public event PropertyChangedEventHandler? PropertyChanged;
protected bool Set<T>(
ref T field,
T newValue,
[CallerMemberName] string? propertyName = null)
{
if (Equals(field, newValue))
{
return false;
}
field = newValue;
PropertyChanged?
.Invoke(
this,
new PropertyChangedEventArgs(propertyName));
return true;
}
}
}