Evgeniy  Kozlov
Evgeniy Kozlov

Reputation: 119

C# SilverLight. The tab key does not change focus for the text boxes

I have got a little problem.

I use ListBox control with the textboxes.

I set focus on the first textbox and try to jump on the following textbox by the key tab. It does not work.

What do I wrong?

Thanks in advance!

<ListBox Name="Box" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Background="Transparent" BorderThickness="0">
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <StackPanel Orientation="Horizontal" Margin="40,2,0,2">
                                    <TextBlock Text="{Binding Label}" MinWidth="20" />
                                    <TextBox  TabIndex="{Binding Index, Mode=OneWay}" Text="{Binding Information, Mode=TwoWay}"/>
                                </StackPanel>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            var model = new List<Model>()
            {
                new Model() {Index = 1, Label = "1"},
                new Model() {Index = 2, Label = "2"},
                new Model() {Index = 3, Label = "3"},
                new Model() {Index = 4, Label = "4"}
            };

            Box.ItemsSource = model;


        }
    }


    public class Model
    {
        public int Index { get; set; }
        public string Label { get; set; }
        public string Information { get; set; }
    }
}

Upvotes: 1

Views: 93

Answers (1)

matt
matt

Reputation: 333

You'll need to specify in the style how you want your tabs to work. You shouldn't need to bind the tabindex, unless you want to change up the order the tab works in. I think this should work similar to what you are trying to do:

<ListBox Name="Box"
         ScrollViewer.HorizontalScrollBarVisibility="Disabled"
         Background="Transparent"
         BorderThickness="0">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <StackPanel Orientation="Horizontal"
                                    Margin="40,2,0,2">
                            <TextBlock Text="{Binding Label}"
                                       MinWidth="20" />
                            <TextBox Text="{Binding Information, Mode=TwoWay}" />
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="IsTabStop"
                    Value="False" />
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.Style>
        <Style TargetType="ListBox">
            <Setter Property="TabNavigation"
                    Value="Cycle" />
        </Style>
    </ListBox.Style>
</ListBox>

Upvotes: 1

Related Questions