如何以编程方式设置Tab键顺序

时间:2016-03-01 14:05:07

标签: c# wpf listbox focus tab-ordering

我正在从代码构建一个窗口。 我添加一个标题TextBlock,然后添加一个listBox的各种元素,其中每个元素都是由 网格---> children [0]一个TextBlock来解释什么是元素      --->子项1用于输入数据的TextBox或组合框。 最后一个按钮。

生成后将焦点放在第一个元素(通常是第一个TextBox)上,以便用户可以立即输入数据。到目前为止一切正常。

- ADD-- 我忘了提到每个元素= Grid = Textblock + TextBox或者combobox,都是父listBox的一个元素。所以我想从每个文本框或组合框中独立于它们所在的列表框元素移动。

enter image description here

之后,我希望用户能够通过TAB移动到下一个元素(文本框或组合框)。因此,当生成以前的窗口时,我使用计数器并设置

tb.TabIndex = tabIndex++;

或     ComboBox.TabIndex = tabIndex ++;

现在的问题是,当我按下TAB时在第一个TextBox中输入数据后,插入符号会丢失,因此焦点...

--- --- ADD2 这是代码:

 private void PreparareTraceFieldGrid(ListBox lbTracefields, ObservableCollection<Tuple<string, object, object>> obc, bool IsReadOnly)
 {
        bool IsFocusSet = false;
        /*---TITOLO---*/
        OutlinedTextBlock otbTitle = CreateOtbForTraceFieldInputWindow(Languages.Word(eWords.InsertTracefields));
        lbTracefields.Items.Add(otbTitle);
        /*----CICLO----*/
        int tabIndex = 0;
        foreach (var item in obc)
        {
            Grid gridInternal = new Grid();
            gridInternal.Background = Brushes.Transparent;

            ColumnDefinition cd1 = new ColumnDefinition();
            cd1.Width = new GridLength(300, GridUnitType.Pixel);
            ColumnDefinition cd2 = new ColumnDefinition();
            cd2.Width = new GridLength(1.0, GridUnitType.Star);
            gridInternal.ColumnDefinitions.Add(cd1);
            gridInternal.ColumnDefinitions.Add(cd2);

            OutlinedTextBlock otb = CreateOtbForTraceFieldInputWindow(item.Item1);
            otb.FontSize = easyRunData.OptionsGraphical.FontSize;
            otb.Stroke = new SolidColorBrush(easyRunData.OptionsGraphical.StrokeColour);
            otb.StrokeThickness = easyRunData.OptionsGraphical.StrokeSize;
            Grid.SetColumn(otb, 0);
            gridInternal.Children.Add(otb);

            string strDataType = item.Item2.ToString();
            eTraceFieldDataTypes eDataType = (eTraceFieldDataTypes)Enum.Parse(typeof(eTraceFieldDataTypes), strDataType);

            switch (eDataType)
            {
                case eTraceFieldDataTypes.STRING:
                case eTraceFieldDataTypes.FLOAT:
                case eTraceFieldDataTypes.INTEGER:
                case eTraceFieldDataTypes.STRING_READONLY:
                    TextBox tb = new TextBox();
                    tb.CaretBrush = Brushes.White;
                    tb.FontSize = easyRunData.OptionsGraphical.FontSize;
                    tb.MinWidth = 200;
                    tb.Height = easyRunData.OptionsGraphical.FontSize + 15;
                    tb.VerticalAlignment = VerticalAlignment.Center;
                    tb.Background = Brushes.Transparent;
                    tb.Margin = new Thickness(10);
                    tb.TabIndex = tabIndex++;
                    tb.KeyDown += (senderEvent, args) => { AnalyzeTraceFields_IF_EMPTY(easyRunData.Tracefields.TraceFieldsUser, (Grid)lbTracefields.Parent); };

                    if (eDataType == eTraceFieldDataTypes.STRING_READONLY && !IsReadOnly)
                        tb.BorderBrush = Brushes.Yellow;
                    else
                        tb.BorderBrush = Brushes.Gainsboro;
                    Grid.SetColumn(tb, 1);
                    gridInternal.Children.Add(tb);
                    lbTracefields.Items.Add(gridInternal);
                    if (!IsFocusSet)
                    {
                        tb.Focus();
                        IsFocusSet = true;
                    }
                    break;
                case eTraceFieldDataTypes.BOOL:
                case eTraceFieldDataTypes.BOOL_READONLY:
                    Border brd = new Border();
                    brd.Margin = new Thickness(10);
                    brd.MinWidth = 200;
                    brd.Height = easyRunData.OptionsGraphical.FontSize + 15;
                    if (eDataType == eTraceFieldDataTypes.BOOL_READONLY && !IsReadOnly)
                        brd.BorderBrush = Brushes.Yellow;
                    else
                        brd.BorderBrush = Brushes.Gainsboro;
                    brd.BorderThickness = new Thickness(3);
                    ComboBox cmb = new ComboBox();
                    cmb.VerticalAlignment = VerticalAlignment.Center;
                    cmb.Background = Brushes.Transparent;
                    cmb.Items.Add(Languages.Word(eWords.True));
                    cmb.Items.Add(Languages.Word(eWords.False));
                    cmb.BorderBrush = Brushes.Gainsboro;
                    cmb.BorderThickness = new Thickness(3);
                    cmb.Margin = new Thickness(0);
                    cmb.TabIndex = tabIndex++;
                    cmb.DropDownClosed += (senderEvent, args) => { AnalyzeTraceFields_IF_EMPTY(easyRunData.Tracefields.TraceFieldsUser, (Grid)lbTracefields.Parent); };
                    Grid.SetColumn(brd, 1);
                    brd.Child = cmb;
                    gridInternal.Children.Add(brd);
                    lbTracefields.Items.Add(gridInternal);
                    if (!IsFocusSet)
                    {
                        cmb.Focus();
                        IsFocusSet = true;
                    }
                    break;
                default:
                    break;
            }
}

0 个答案:

没有答案