我有一个自定义文本框控件,当其中没有文本时会显示其名称,但出于某种奇怪的原因,我必须按两次制表符才能从前一个元素进入控件的文本字段。在第一个选项卡上,它突出显示的是 TextBox 的边框。我在打开属性窗口并搜索“选项卡”的情况下浏览了 Generic.xaml 文件的所有级别,但我能找到的唯一一个是 TextBox 本身,它是正确的制表符。如何让我的控件只需要一个选项卡即可进入
通用的.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); }
}
}
}