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

时间:2015-07-25 02:35:23

标签: wpf xaml wpf-controls

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

但是文本作为背景的位置不是基本TextBox的文本的一部分是不正确的。 enter image description here 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
}
}
我预料到: enter image description here

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

1 个答案:

答案 0 :(得分:1)

解决此问题的一种方法是使用TextBox作为visual而不是TextBlock。此TextBox必须与原始BorderThickness具有相同的SizeTextBoxBase。因此,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;
    }
}