C# 如何在没有单击事件的情况下使特定 TabItem 获得对 TabControl 的关注?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1227132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:56:02  来源:igfitidea点击:

How can I make a specific TabItem gain focus on a TabControl without click event?

c#wpfxamlfocustabcontrol

提问by Edward Tanguay

How can I tell my TabControl to set the focus to its first TabItem, something like this:

我如何告诉我的 TabControl 将焦点设置到它的第一个 TabItem,如下所示:

PSEUDO-CODE:

伪代码:

((TabItem)(MainTabControl.Children[0])).SetFocus();

采纳答案by Martin Liversage

How about this?

这个怎么样?

MainTabControl.SelectedIndex = 0;

回答by Chernikov

tabControl1.SelectedTab = item;
item.Focus();

回答by Ray

tabControl.SelectedItem = tabControl.Items[0];

tabControl.SelectedItem = tabControl.Items[0];

回答by Webking

If you have a Tabcontroller named tabControl you could set the selectedIndex from different methods, i use following methods mostly.

如果您有一个名为 tabControl 的 Tabcontroller,您可以从不同的方法设置 selectedIndex,我主要使用以下方法。

codebehind:

代码隐藏:

tabControl.SelectedIndex = 0; // Sets the focus to first tabpanel

clientside:

客户端:

First, put the following javascript in your aspx/ascx file:

首先,将以下 javascript 放入您的 aspx/ascx 文件中:

<script type="text/javascript">
function SetActiveTab(tabControl, activeTabIndex) {
    var activeTab = tabControl.GetTab(activeTabIndex);
    if(activeTab != null)
        tabControl.SetActiveTab(activeTab);
}</script>

Then add following clientside event to prefered controller:

然后将以下客户端事件添加到首选控制器:

OnClientClick="function(s, e) { SetActiveTab(tabControl, 0);

回答by Brett Ryan

I realise this was answered a long time ago, however a better solution would be to bind your items to a collection in your model and expose a property that selected item is bound to.

我意识到很久以前就已经回答了这个问题,但是更好的解决方案是将您的项目绑定到模型中的集合并公开所选项目绑定到的属性。

XAML:

XAML:

<!-- MyTemplateForItem represents your template -->
<TabControl ItemsSource="{Binding MyCollectionOfItems}"
            SelectedItem="{Binding SelectedItem}"
            ContentTemplate="{StaticResource MyTemplateForItem}">
</TabControl>

Code Behind:

背后的代码:

public ObservableCollection<MyItem> MyCollectionOfItems {
    get;
    private set;
}

private MyItem selectedItem;
public MyItem SelectedItem{
    get { return selectedItem; }
    set {
        if (!Object.Equals(selectedItem, value)) {
            selectedItem = value;
            // Ensure you implement System.ComponentModel.INotifyPropertyChanged
            OnNotifyPropertyChanged("SelectedItem");
        }
    }
}

Now, all you have to do to set the item is:

现在,设置项目所需要做的就是:

MyItem = someItemToSelect;

You can use the same logic with the SelectedIndexproperty, further, you can use the two at the same time.

您可以对SelectedIndex属性使用相同的逻辑,此外,您可以同时使用两者。

This approach allows you to separate your model correctly from the UI, which could allow you to replace the TabControlwith something else down the line but not requiring you to change your underlying model.

这种方法允许您正确地将模型与 UI 分开,这可以让您TabControl用其他东西替换 UI,但不需要您更改底层模型。

回答by V G S Naidu

it's better to use the following type of code to select the particular item in the particular tab...

最好使用以下类型的代码来选择特定选项卡中的特定项目...

.

.

 private void PutFocusOnControl(Control element)
        {
            if (element != null)
                Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Input,
                    (System.Threading.ThreadStart)delegate
                    {
                        element.Focus();
                    });
        }

And in calling time... tabcontrol.isselected=true; PutFocusOnControl(textbox1);

will works fine...

而在调用的时候... tabcontrol.isselected=true; PutFocusOnControl(textbox1);

会很好用...

回答by Brian

this.tabControl1.SelectedTab = this.tabControl1.TabPages["tSummary"];

I've found it's usually a best practice to name your tabs and access it via the name so that if/when other people (or you) add to or subtact tabs as part of updating, you don't have to go through your code and find and fix all those "hard coded" indexes. hope this helps.

我发现通常最好的做法是命名您的标签并通过名称访问它,这样如果/当其他人(或您)添加或删除标签作为更新的一部分时,您就不必通过您的代码并找到并修复所有那些“硬编码”索引。希望这可以帮助。

回答by Resmi P R

Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TabControl1.SelectedIndexChanged 'MsgBox(TabControl1.SelectedIndex)

Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) 处理 TabControl1.SelectedIndexChanged 'MsgBox(TabControl1.SelectedIndex)

    If TabControl1.SelectedIndex = 0 Then
        txt_apclntFrstName.Select()
    Else
        txtApplcnNo.Select()
    End If


End Sub

回答by Chris Catignani

Look at the properties for the tab control... Expand the TabPages properties "collection"... Make note of the names you gave the members.

查看选项卡控件的属性... 展开 TabPages 属性“集合”... 记下您为成员指定的名称。

ie. a tab control called tabMain with 2 tabs called tabHeader and tabDetail

IE。一个名为 tabMain 的选项卡控件,带有 2 个名为 tabHeader 和 tabDetail 的选项卡

Then to select either tab...You have to set it with the tabname

然后选择任一选项卡...您必须使用选项卡名称进行设置

tabMain.SelectedTab = tabHeader;

回答by Scott Solmer

Basically all of the answers here deal with SELECTION, which does not answer the question.
Maybe that is what OP wanted, but the question very specifically asks for FOCUS.

基本上这里的所有答案都涉及SELECTION,它没有回答问题。
也许这就是 OP 想要的,但这个问题非常具体地要求FOCUS

TabItem item = (TabItem)MainTabControl.Items[0];
// OR
TabItem item = (TabItem)MainTabControl.SelectedItem;
// Then
item.Focus();