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

给你一囗甜甜゛ 提交于 2020-05-12 11:23:07

问题


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

PSEUDO-CODE:

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

回答1:


How about this?

MainTabControl.SelectedIndex = 0;



回答2:


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.




回答3:


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:

<!-- 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 SelectedIndex property, further, you can use the two at the same time.

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




回答4:


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

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

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

tabMain.SelectedTab = tabHeader;



回答5:


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



回答6:


tabControl.SelectedItem = tabControl.Items[0];




回答7:


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

codebehind:

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

clientside:

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

<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);



回答8:


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...




回答9:


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

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


End Sub



回答10:


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.

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


来源:https://stackoverflow.com/questions/1227132/how-can-i-make-a-specific-tabitem-gain-focus-on-a-tabcontrol-without-click-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!