WPF自定义文本框需要两个选项卡才能获取文本? [英] WPF custom textbox takes two tabs to get to text?

查看:77
本文介绍了WPF自定义文本框需要两个选项卡才能获取文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义文本框控件,当其中没有文本时会显示其名称,但是出于某些奇怪的原因,我必须两次单击Tab键才能从上一个元素中获取该控件的文本字段。在第一个标签上,突出显示文本框的边框。我浏览了Generic.xaml文件的所有级别,并打开了属性窗口,并搜索 tab,但我唯一能找到的就是TextBox本身,它已经可以正确制表了。如何使控件仅使用一个选项卡进入

I have a custom textbox control that displays its name when there is no text inside of it, but for some curious reason I have to hit tab twice to get from the previous element to get into the control's text field. On the first tab it highlight's the TextBox's border. I went through all of the levels of the Generic.xaml file with the properties window open and searching for 'tab' but the only one that I could find was on the TextBox itself which is properly tabstopping. How do I make my control only take one tab to get into the

Generic.xaml:

Generic.xaml:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SuperTB">
<Style TargetType="{x:Type local:SuperTextB}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:SuperTextB}">
                <Border Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">
                    <TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Text, Mode=TwoWay, UpdateSourceTrigger=LostFocus }" x:Name="PART_input">
                    </TextBox>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

CS:

    [TemplatePart(Name="PART_input")]
public class SuperTextB : Control
{
    private TextBox PART_input;

static SuperTextB()
{
    DefaultStyleKeyProperty.OverrideMetadata(typeof(SuperTextB), new FrameworkPropertyMetadata(typeof(SuperTextB)));
}

public SuperTextB()
{
    Loaded += SuperTextBLoaded;
}

void SuperTextBLoaded(object sender, RoutedEventArgs e)
{
    if (PART_input.Text == string.Empty)
    {
        PART_input.Background = convertName();
    }
}


    public override void OnApplyTemplate()
    {
        PART_input = GetTemplateChild("PART_input") as TextBox;
        if (PART_input != null)
        {
            PART_input.GotFocus += PartInputGotFocus;
            PART_input.LostFocus += PartInputLostFocus;
        }
    }

    void PartInputLostFocus(object sender, RoutedEventArgs e)
    {
        if (PART_input.Text == string.Empty)
        {
            PART_input.Background = convertName();
        }
    }

    private VisualBrush convertName()
    {
        char[] pieces = Name.ToCharArray();
        for (int x = 0; x < pieces.Length; x++)
        {
            if (pieces[x].Equals('_'))
                pieces[x] = ' ';
        }

        String toReturn = "";

        foreach (char c in pieces)
            toReturn += c.ToString();

        VisualBrush myVis = new VisualBrush();
        myVis.Stretch = Stretch.None;
        TextBlock myText = new TextBlock();
        myText.Text = toReturn;
        myText.Foreground=Brushes.Gray;
        myVis.Visual=myText;
        return myVis;
    }


    void PartInputGotFocus(object sender, RoutedEventArgs e)
    {
            PART_input.Background = Brushes.White;
    }


    public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(SuperTextB));

    public string Text
    {
        get { return (String)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
}

}

推荐答案

这是因为您的 TextBox TextBox c $ c>。 TextBox 是可聚焦的,外部的 TextBox 也是可聚焦的。将 IsFocusable 设置为 False 或更改模板,使其不包含 TextBox

It's because you have a TextBox within the template for your TextBox. That TextBox is focusable, as is your outer TextBox. Set IsFocusable to False or alter your template such that it doesn't include a TextBox within it.

这篇关于WPF自定义文本框需要两个选项卡才能获取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆