Featured Post

Organize and rename photos by EXIF data with PowerShell

This PowerShell script organizes and renames all photos in a selected folder using EXIF data. It will also create thumbnails of the images i...

Friday, December 27, 2013

Change TabControl SelectedTabItem in your ViewModel by TabItem Name or Header Value

Using these simple methods you can change your active tab in your ViewModel via commands or methods. If you want to change to a tab but don't want to have to track the tab index, you simply use the method that accepts the Tab Name or Header string name to change to that tab.

XAML:
<TabControl x:name="MyTabControl">...</TabControl>

Code Behind:
public MyUserControlorViewConstructor()
{
    InitializeComponent();
    
    MyViewModel mvm = new MyViewModel();
    DataContext = mvm;
    
    mvm.MyTabControl = MyTabControl;    
}

ViewModel:
public TabControl MyTabControl { get; set; }

public static void SetSelectedTab(string tabName)
{
    for (int i = 0; i < MyTabControl.Items.Count; i++)
    {
        TabItem item = MyTabControl.Items.GetItemAt(i) as TabItem;
        if (null == item || (item.Name != tabName && item.Header.ToString() != tabName)) continue;  
        MyTabControl.SelectedIndex = i;
        item.IsSelected = true;
        return;
    }
}

public static void SetSelectedTab(int tabIndex)
{
    TabItem item = MyTabControl.Items.GetItemAt(tabIndex) as TabItem;
    if (item == null) return;
    item.IsSelected = true;
    MyTabControl.SelectedIndex = tabIndex;
}

Sunday, December 8, 2013

All I want for Xmas is bitcoin!