如何在 TextBox 中正确显示文本作为背景?

How to show text as background in TextBox correctly?

我有一个文本框和一个显示为背景的文本提示:

但作为背景的文本位置不正确,它不是基本文本框文本的一部分。 xaml:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:v="clr-namespace:WpfApplication2"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="152,19,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"
             v:Autocomplete.Hint ="abcd"/>


</Grid>

Autocomplete.cs:

namespace WpfApplication2
{
public static class Autocomplete
{
    #region Hint
    public static string GetHint(DependencyObject obj)
    {
        return (string)obj.GetValue(HintProperty);
    }

    public static void SetHint(DependencyObject obj, string value)
    {
        obj.SetValue(HintProperty, value);
    }

    public static readonly DependencyProperty HintProperty =
        DependencyProperty.RegisterAttached("Hint", typeof(string), typeof(Autocomplete), new PropertyMetadata(string.Empty, OnTextBoxBaseFocus));

    private static string hintText = string.Empty;

    private static void OnTextBoxBaseFocus(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        TextBoxBase txtBase = (TextBoxBase)d;

        hintText = GetHint(d);
        if (txtBase == null)
            return;

        if ((string)e.NewValue != null && !GetHint(d).Contains(" "))
        {
            txtBase.GotFocus += txtBase_GotFocus;
            txtBase.LostFocus += txtBase_LostFocus;
        }
        else
            txtBase.TextChanged -= OnChanged;
    }

    static void txtBase_GotFocus(object sender, RoutedEventArgs e)
    {
        AutocompleteText(sender);
    }

    static void txtBase_LostFocus(object sender, RoutedEventArgs e)
    {
        TextBoxBase txtBase = (TextBoxBase)sender;
        // Hide Autocomplete hint
        txtBase.Background = null;
    }




    private static void OnChanged(object sender, TextChangedEventArgs e)
    {
        AutocompleteText(sender);
    }

    private static void AutocompleteText(object sender)
    {
        TextBoxBase txtBase = (TextBoxBase)sender;
        if (txtBase != null && txtBase.Focus())
        {
            // Show Autocomplete hint
            var visual = new TextBlock()
            {
                FontStyle = FontStyles.Normal,
                Text = hintText,
                Foreground = Brushes.Gray
            };

            txtBase.Background = new VisualBrush(visual)
            {
                Stretch = Stretch.None,
                AlignmentX = AlignmentX.Left,
                AlignmentY = AlignmentY.Center,
                Transform = new TranslateTransform(3, 0)
            };
        }
        else
        {
            // Hide Autocomplete hint
            txtBase.Background = null;
        }
    }

    #endregion
}
}

我预计:

如何按预期显示 Autocomplete.Hint?谢谢帮助!

解决此问题的一种方法是使用 TextBox 作为您的 visual 而不是 TextBlock。这个TextBox必须和原来的TextBoxBase有相同的BorderThickness和相同的Size。因此,您的 AutocompleteText 方法应更改为:

private static void AutocompleteText(object sender)
{
    TextBoxBase txtBase = (TextBoxBase)sender;
    if (txtBase != null && txtBase.Focus())
    {
        // Show Autocomplete hint
        var visual = new TextBox()
        {
            BorderThickness = txtBase.BorderThickness,
            BorderBrush = Brushes.Transparent,
            Width = txtBase.ActualWidth,
            Height = txtBase.ActualHeight,

            Text = hintText,
            Foreground = Brushes.Gray
        };

        txtBase.Background = new VisualBrush(visual)
        {
            Stretch = Stretch.None,
        };
    }
    else
    {
        // Hide Autocomplete hint
        txtBase.Background = null;
    }
}