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

Wpf 如何在文本框中正确显示文本作为背景?,wpf,xaml,wpf-controls,Wpf,Xaml,Wpf Controls,我有一个文本框和一个文本提示,显示为背景: 但是作为背景的文本的位置不正确,它不是基本文本框文本的一部分。 xaml: 我期望: 如何显示自动完成。按预期提示?谢谢您的帮助 解决这个问题的一种方法是使用文本框作为视觉,而不是文本块。此TextBox必须具有与原始TextBoxBase相同的边框厚度和大小。因此,您的AutocompleteText方法应该更改如下: private static void AutocompleteText(object sender) { TextBox

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

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

我期望:


如何显示自动完成。按预期提示?谢谢您的帮助

解决这个问题的一种方法是使用
文本框
作为
视觉
,而不是
文本块
。此
TextBox
必须具有与原始
TextBoxBase
相同的
边框厚度和
大小。因此,您的
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;
    }
}

尝试将
Padding=“0”
添加到文本框中。它将文本向上和向左移动了一点。@Dave,谢谢你的回复。现在看起来很棒!
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
}
}
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;
    }
}