0

I have created a ControlTemplate for a TextBox that includes a label for it. However, When I try to use Tab to navigate the controls I have to press tab twice to enter the textbox field, as if it's focusing on another element inside. I tried messing the labels focusability and what not but that doesn't seem to be the issue. Here is the code:

<ControlTemplate x:Key="custTextbox" TargetType="{x:Type TextBox}">
    <Canvas x:Name="customTextbox">
        <Border CornerRadius="3, 0, 0 ,3"  BorderThickness="1, 1, 0, 1" 
                Height="30" x:Name="brdTextboxLabel" Width="98">
            <Border.BorderBrush>
                <LinearGradientBrush StartPoint=".5,0" EndPoint=".5,1">
                    <GradientStop Color="#3C3F48" Offset=".88"/>
                    <GradientStop Color="#9CA1A8" Offset=".96"/>
                </LinearGradientBrush>
            </Border.BorderBrush>
            <Border.Background>
                <LinearGradientBrush StartPoint=".5, 0" EndPoint=".5, 1">
                    <GradientStop Color="#414447" Offset="0"/>
                    <GradientStop Color="#4E525B" Offset=".08"/>
                </LinearGradientBrush>
            </Border.Background>
            <Canvas>
                <Rectangle Height="24" x:Name="rectangle3" Stroke="#636369" 
                           Width="1" Canvas.Left="96" Canvas.Top="2" />
                <Label Canvas.Left="0" Padding="9,6.5,0,0" Foreground="#BABBBF" 
                       FontWeight="Bold" Canvas.Top="0" FontSize="11" 
                       Content="{TemplateBinding Tag}" Height="28" 
                       x:Name="lblTextboxHeader" Width="92" />
            </Canvas>
        </Border>
        <!-- ========================================= -->
        <Border CornerRadius="0,3,3,0" BorderThickness="0,1,1,1" Canvas.Left="98" 
                Height="30" x:Name="brdTextbox" Width="348">
            <Border.BorderBrush>
                <LinearGradientBrush StartPoint=".5,0" EndPoint=".5,1">
                    <GradientStop Color="#3C3F48" Offset=".88"/>
                    <GradientStop Color="#9CA1A8" Offset=".96"/>
                </LinearGradientBrush>
            </Border.BorderBrush>
            <Border.Background>
                <LinearGradientBrush StartPoint=".5, 0" EndPoint=".5, 1">
                    <GradientStop Color="#414447" Offset="0"/>
                    <GradientStop Color="#4E525B" Offset=".08"/>
                </LinearGradientBrush>
            </Border.Background>
            <Canvas>
                <TextBox TabIndex="0" Background="Transparent" CaretBrush="#8C8CA1"
                         FontSize="16" Padding="4, 3, 0 ,0" BorderBrush="Transparent" 
                         Foreground="#D4D5DA" Canvas.Left="0" Canvas.Top="-1" 
                         Height="30" x:Name="textBox1" Width="347"/>
            </Canvas>
        </Border>
    </Canvas>
</ControlTemplate>

Sorry if it's a mess of a ControlTemplate, it was the first I had ever made when starting wpf/xaml.

Thank you for your time!

Brian Crawford
  • 123
  • 1
  • 10
  • 1
    Download Snoop and run it against your application. Hit tab and watch the bottom left corner of it and it will tell you what has focus. After figuring out what has focus, set the `IsTabStop` property on that element. – Kcvin Feb 19 '15 at 14:41
  • I'm on a business computer, I can't just go download something. Thank you though! – Brian Crawford Feb 19 '15 at 14:46
  • While you're at it, find a new business ;) I'm kidding, I'm checking for you right now. – Kcvin Feb 19 '15 at 14:48
  • The TextBox gets focus on the first tab when this XAML is placed inside an empty Window. With that being said, something else outside of this XAML is grabbing focus. – Kcvin Feb 19 '15 at 14:52
  • huh weird...I have other controls that get focus. Even a non-templated textbox does. I'll poke around with it some – Brian Crawford Feb 19 '15 at 14:53
  • I had to make two adjustments when running your XAML though, 1 was removing the `CaretBrush` and the other was removing the `TemplateBinding Tag`. Elements dealing with those properties may very well be getting focus. – Kcvin Feb 19 '15 at 14:56
  • Removed them and still no go, this is just weird. Thanks for your help though. – Brian Crawford Feb 19 '15 at 15:02
  • We could discuss more in [WPF chat](http://chat.stackoverflow.com/rooms/18165/wpf) if you'd like. Would need more XAML to get your question answered in it current form. – Kcvin Feb 19 '15 at 15:12
  • I got it, posting answer – Brian Crawford Feb 19 '15 at 15:14

1 Answers1

2

HA! Figured it out while solving an issue on another control.

The problem was I, essentially, had 2 TextBoxes each time I used the template.

a simple map would be:

<TextBox>
    <ControlTemplate>
        <Label/>
        <Textbox/>
    </ControlTemplate>
</TextBox>

So I just had to make the control I put in the window have KeyboardNavigation.IsTabStop="false" So it would pass that textbox and go to the one inside my ControlTemplate.

Brian Crawford
  • 123
  • 1
  • 10
  • I would reevaluate your design if I were you. The outer TextBox isn't being used it seems? so why have it? Are you trying to make a TextBox similar to SO where you can enter keywords ( like [wpf]) which render into labels? – Kcvin Feb 19 '15 at 16:52
  • [ScreenShot](http://i.cubeupload.com/7HjscE.png) Basically, I am using the ' Content="{TemplateBinding Tag}" ' To set what is displayed by the Label within the ControlTemplate. This might be (read: probably is) a wonky way of getting what I want accomplished. Maybe this would have been better as a UserControl? – Brian Crawford Feb 19 '15 at 17:08
  • 404 Not Found on image. – Kcvin Feb 19 '15 at 17:13
  • buh, work for me, trying another host: [Link Again](http://uploadpie.com/c8SOx) (I can't access imgur on my work computer...unfortunately) – Brian Crawford Feb 19 '15 at 17:15
  • I see you're trying to use a reusable control, so UserControl or even something like Grid or StackPanel would work. You simply have a Grid with a few columns. What you don't have is a TextBox with a Label and another TextBox. There are a few things you could do but that should probably be researched or asked in another question. – Kcvin Feb 19 '15 at 17:32